Specifications
else
{
echo “<p>You are not logged in.</p>”;
echo “<p>Only logged in members may see this page.</p>”;
}
echo “<a href=\”authmain.php\”>Back to main page</a>”;
?>
This code is very simple. All it does is start a session, and check if the current session contains
a registered user using the session_registered_user() function. If the user is logged in, we
show her the members content; otherwise we tell her that she is not authorized.
Finally we have the logout.php script that signs a user out of the system. The code for this
script is shown in Listing 20.6.
LISTING 20.6 logout.php—This Script Deregisters the Session Variable and Destroys the
Session
<?
session_start();
$old_user = $valid_user; // store to test if they *were* logged in
$result = session_unregister(“valid_user”);
session_destroy();
?>
<html>
<body>
<h1>Log out</h1>
<?
if (!empty($old_user))
{
if ($result)
{
// if they were logged in and are not logged out
echo “Logged out.<br>”;
}
else
{
// they were logged in and could not be logged out
echo “Could not log you out.<br>”;
}
}
Advanced PHP Techniques
P
ART IV
444
LISTING 20.5 Continued
25 7842 CH20 3/6/01 3:42 PM Page 444










