How Do You Create an Option to Logout for a PHP Web Page to Destroy the Session?

Problem scenario
You have a LAMP (or LAPP) web page application.  The PHP code uses the session() functionality.  Once a user logs in successfully, he/she can browse to successive web pages.  You want to at least allow the user the opportunity to destroy the session (and terminate the authentication session).  How do you do this?

Solution
#1  Modify the .php file that you want to have the logout feature.  In the <html> <body> section of your .php file, add this line:

<a href="logout.php">Logout</a>

#2  Create a file named logout.php in the same directory as the .php file referred to in step #1.  Have this be the content of the logout.php file:

session_start();
session_destroy();
echo 'You have been logged out.;<a href="/path/to/login.php">Log in again.<a/>';
exit;

Note that /path/to/ is a subdirectory path of /var/www/html/ (or the default location of the web files).  login.php is the name of the .php file in /var/www/html/path/to/ that allows for logins.

Leave a comment

Your email address will not be published. Required fields are marked *