Cramerz
  • ASP.NET
  • C++
  • CSS
  • HTML
  • Java
  • JavaScript
  • PHP
  • Python
  • SQL
  • WordPress
  • Other Courses
    • Computer Basics
    • Database Concepts
    • Dreamweaver
    • English Agency Law
    • English Company Law
    • Flash
    • MCSE
    • Networking Basics
    • Photoshop
    • XML
Select Page

PHP Logout

0 comments

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:

  • a logout link from a restricted page
  • a php script to check if the user logged out (and eventually redirect to the login page)

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 -->

 

PHP Logout

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:

  • a logout link from a restricted page
  • a php script to check if the user logged out (and eventually redirect to the login page)

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

PHP
1
2
3
<?php #admin/restricted.php
          // [...]
?>

1
2
3
<!-- 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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
 
?>

1
2
3
4
5
<!-- 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 -->

 

PHP

Copyright © 2025 by Cramerz.