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

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

  • Php Basics

  • What Is PHP
  • PHP Tags
  • PHP Comments
  • PHP Output Data
  • PHP Variables
  • PHP Data Types
  • PHP Data Types
  • PHP If Else
  • PHP Switch
  • PHP Arrays
  • PHP Operators
  • PHP Include
  • PHP Loops
  • PHP Functions
  • PHP Send Emails
  • PHP Constants
  • Php Global Variables

  • PHP Global Variables
  • PHP Get
  • PHP Post
  • PHP Session
  • PHP Cookie
  • PHP Files
  • PHP Request
  • PHP Env
  • PHP Server
  • Php Mysql Database

  • PHP Create And Drop A DB
  • PHP Create A DB Table
  • PHP Database Queries
  • PHP Connect To Db
  • PHP Insert To Db
  • PHP Select From Db
  • PHP Delete From DB
  • PHP Update Db
  • PHP Database Security
  • Php Login Project

  • PHP Login System
  • PHP Logout
  • PHP OOP

  • Object Oriented Programming
  • PHP Properties And Methods
  • PHP Objects And Classes
  • PHP The Constructor Method
  • PHP Inheritance

Copyright © 2023 by Cramerz.