Completing the Login Script for a PEAR CMS - The logout script
(Page 3 of 4 )
This script is responsible for logging a user out of the system. In programming terms, the script ends a user session, which is started when the session_start() function is used.
Below is the code that makes up the script:
<?php
session_start();
if(isset($_SESSION['author_name'])) {
session_unset();
session_destroy();
header("location:login.php" );
exit();
}
else{
if(!isset($_SESSION['author_name'])) {
//the session variable isn't registered, the user shouldn't even be on this page
header("location:login.php" );
exit();
}
}
?>
The code starts by opening a session. This is achieved by calling the session_start() function. This function is used to keep track of session variables such as the ones we set up in the login script. Then we check to see if the session variable called author_name is set:
<?php
session_start();
if(isset($_SESSION['author_name'])) {
Then you call either the session_destroy() or session_unset() functions (depending on your preference) which empty out the session variables or deletes them and then redirects the user to the login page:
session_unset();
session_destroy();
header("location:login.php" );
exit();
}
If the session variable is not set, then the user is on the wrong page or was not logged in in the first place, in which case the user is also redirected to the login page:
else{
if(!isset($_SESSION['author_name'])) {
//the session variable isn't registered, the user shouldn't even be on this page
header("location:login.php" );
exit();
Next: The CMS >>
More PEAR Articles Articles
More By David Web