User Authentication With patUser (part 3) - The History Channel (
Page 3 of 7 )
patUser allows you to
track user movement through your application with a set of history functions,
which maintain a buffer of all the URLs visited and provide you with hooks to
return to any of them. As you might imagine, this comes in handy if you need to
provide users with controls to go back to the previous page, or to show them
their browsing history.
The primary method to accomplish this is the
keepHistory() method, a call to which should be included on every page of your
application. The function accepts two parameters, the size of the history buffer
and the title of the current page, and stores the current URL and supplied title
in the session for easy retrieval.
Here's a sample of what that history
buffer might look like:
Array
(
[0] => Array
(
[url] => /add_user.php?
[title] => Add User
)
[1] => Array
(
[url] => /patuser/modify_user.php?
[title] => Modify User
)
[2] => Array
(
[url] => /scripts/review.php?
[title] => Verify User
)
)
Once the history buffer has been created, data from it may be
retrieved via the getHistory() method, which returns an array holding a list of
all the URLs and pages visited by the user in the current session. The following
example illustrates, using the sample data above as reference.
<?php
// include classes
include("../include/patDbc.php"); include("../include/patUser.php");
// initialize database layer
$db = new patMySqlDbc("localhost", "db211", "us111", "secret");
// initialize patUser
$u = new patUser(true);
// connect patUser to database
$u->setAuthDbc($db);
// set tables
$u->setAuthTable("users");
// get history
$history = $u->getHistory();
// iterate over array
// display history with clickable URLs
echo "<h2>Your history:</h2>";
echo "<ol>";
foreach ($history as $h)
{
echo "<li><a href=" . $h['url'] . ">" . $h['title'] . "</a>"; } echo "</ol>";
?>
An additional goHistory() method makes it possible to send
the browser back to a specific page in the buffer via HTTP redirection. Consider
the following script, which accepts a number and redirects the browser back that
many pages (note that the goHistory() method must be provided with negative
values as input):
<?php
// include classes
include("../include/patDbc.php"); include("../include/patUser.php");
// initialize database layer
$db = new patMySqlDbc("localhost", "db211", "us111", "secret");
// initialize patUser
$u = new patUser(true);
// connect patUser to database
$u->setAuthDbc($db);
// set tables
$u->setAuthTable("users");
// go back i pages
$u->goHistory($_GET['i'] * -1);
?>