Jump to content
Larry Ullman's Book Forums

Pg 395 - Logout.Php (Improvement To The Logging Out Process?)


Recommended Posts

From pg 395 of the book:

 

"Unlike when using the cookie version of the logout.php script, you cannot refer to the user by their first name anymore, as all data has been deleted."

 

What if we don't like this method?  If we would prefer to provide the user with a more personalized logout message (rather than "You are now logged out!), couldn't we take the value of the user's name ( $_SESSION['first_name'] ) and stuff it into a variable prior to deleting the session, and then use that variable within the logout message?

 

I think this works below - do you see any issue with what I did here?:

<?php # Script 12.11 - logout.php #2
// This page lets the user log out
// This version uses sessions

session_start(); // Access the existing session.

// If no Session Variable is present, redirect the user
if (!isset($_SESSION['user_id'])) {
	
	// Need the functions
	require ('includes/login_functions.inc.php');
	redirect_user();
	
} else { // Cancel the session
	
	// *** ADDITIONAL CODE ADDED BY ME:  
	// *** Prior to deleting the session and access to first_name, 
	// *** add the value to another new variable to preserve its use when logging out:
	$users_first_name = $_SESSION['first_name'];
	
	$_SESSION = array(); // Clear the variables
	session_destroy(); // Destroy the session itself
	setcookie ('PHPSESSID', '', time()-3600, '/', '', 0, 0); // Destroy the session's cookie
	
}  //  At this point we now longer have access to session info / cookie info, but we do have access to the user's first name via $users_first_name

// Set the page title and include the HTML Header
$page_title = 'Logged Out!';
include ('includes/header.html');

// Print a customized message
echo "<h1>Logged Out!</h1>
<p>You are now logged out, $users_first_name!</p>";   


include ('includes/footer.html');
?>
Link to comment
Share on other sites

 Share

×
×
  • Create New...