Deleting a Session

You want to delete a session. Session data exists in two areas, so you'll need to delete both.

Use session_start(), unset() to delete the session variables, and session_destroy() to remove the session data from the server.

<?php
// Enable output buffering. No output is sent from the script
// (other than headers). It is saved in an internal buffer
ob_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>Deleting a Session</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php

// Start the session
session_start();
// Delete the session variables
unset ($_SESSION);
// Destroy the session data
session_destroy();
echo "<p>You are now logged out. Have a nice day.</p>";
echo "<p><a href=\"example_c.php\">
Create the session variables?</a></p>";

?>
</p>
</body>
</html>
<?php
// Flush the buffer and end output buffering.
ob_end_flush();
?>

View the effect


Sessions in PHP | Introduction | Basic Session Functionality | Creating a Session | Accessing Session Variables | Deleting a Session | Implementing a Login System with Sessions | Saving Arrays in a Session | Using Cookies with Session | Sessions without Cookies | Potential Session Problems
© 2008: Hann So
email: hso@voyager.deanza.edu