Home arrow PHP arrow Page 4 - Authentication Scripts for a User Management Application

The logout script - PHP

In this article we will continue to discuss the application-wide scripts that we started to talk about in the last article. These special scripts are used by all the scripts and pages of the application. We will continue to look at the func.inc script that has several useful functions defined in it. This article is the third part of a nine-part series.

TABLE OF CONTENTS:
  1. Authentication Scripts for a User Management Application
  2. Script Explained
  3. Function Continued
  4. The logout script
By: David Web
Rating: starstarstarstarstar / 7
December 01, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The logout script is responsible for ending a session that the user starts when he or she is logged in. It has the following code:


<?php

session_start();


if(isset($_SESSION['uname'])) {

session_unset();

session_destroy();

header("location:login.php" );

exit();

}

else{

if(!isset($_SESSION['uname'])) {

//the session variable isn't registered, the user shouldn't even be on this page

header("location:login.php" );

exit();

}

}

?>


It ends sessions in the following way. First, it checks to see if the username session variable is set after opening the session by calling the session_start() function:


<?php

session_start();


Then the username check is carried out:


if(isset($_SESSION['uname'])) {


Next, the session_unset() function is called. This function essentially resets the session array:


session_unset();


At this point, the session_destroy() functions are called to end the session:


session_destroy();


The session_destroy() function essentially removes the session data from the server, where it is stored in temporary files. Once the session has been destroyed, the user is redirected to the login page:


header("location:login.php" );

exit();

}


If the username session variable is not set, then the user is also redirected to the login page, since he or she does not have the right to be on the page in the first place: 

else{

if(!isset($_SESSION['uname'])) {

//the session variable isn't registered, the user shouldn't even be on this page

header("location:login.php" );

exit();

}

}

?>

In the next article, we will take a close look at the login page and the script behind this essential function. See you next week!



 
 
>>> More PHP Articles          >>> More By David Web
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: