home

Php Logout Tutorial


Before creating the logout, make sure you have a working login system. We will assume we have all the login files ready, so we will start from STEP4 of login tutorial (restricted page)

To make your user log out from the restricted area you will need:

STEP1.  Add a logout link in the HTML of the restricted page:

<?php #admin/restricted.php 
// [...]

?>
<!-- RESTRICTED PAGE HTML GOES HERE -->
<!-- add a LOGOUT link before the form -->
<p>{ <a href="?log=out">log out</a> }</p>
 

The log out link will add aquery string like ?log=out. It will send a log variable to the same page with a GET method.

Inside the php script at the top of the restricted page you can now handle the log variable using the $_GET superglobal.

STEP2.  Check if the user logged out and eventually redirect to login page:

<?php #admin/restricted.php 
#####[make sure you put this code before any html output]#####

//starting the session
session_start();

//checking if a log SESSION VARIABLE has been set
if( !isset($_SESSION['log']) || ($_SESSION['log'] != 'in') ){
//if the user is not allowed, display a message and a link to go back to login page
echo "You are not allowed. <a href="index.php">back to login page</a>";

//then abort the script
exit();
}

#### CODE FOR LOG OUT ####
if(isset($_GET['log']) && ($_GET['log']=='out')){
//if the user logged out, delete any SESSION variables session_destroy();

//then redirect to login page
header('location:index.php');
}//end log out


?>
<!-- RESTRICTED PAGE HTML GOES HERE -->
<!-- add a LOGOUT link before the form -->
<p>{ <a href="?log=out">log out</a> }</p>

 <!-- RESTRICTED PAGE HTML GOES HERE -->

 


Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Copyright © 2014 by Cramerz.com.