PHP Logout Script

PHP Logout Script

Php logout script

Before creating the logout, make sure you have a working with PHP login Script.

To make your user log out from the restricted area you will need:
a logout link from a restricted page
a php script to check if the user logged out then redirect to the login page or index page.

Logout php script

 <?php 
restricted.php 
 
//starting the session
session_start();

//checking if a log SESSION VARIABLE has been set or not.
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 or index page.
 echo "You are not allowed. <a href="index.php">back to login page</a>";
 
 exit();
}
 // CODE FOR USER LOG OUT 
if(isset($_GET['log']) && ($_GET['log']=='out')){
 //if the user logged out then 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>
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.