Google News
logo
PHP Program to Destroy a session
To destroy a PHP session, you need to unset all the session variables and then call the `session_destroy()` function. Here's an example program that demonstrates how to destroy a session :
Program :
<?php
// Start the session
session_start();

// Set session variables
$_SESSION["username"] = "john_doe";
$_SESSION["email"] = "john_doe@example.com";

// Print the session variables
echo "Before destroying session:<br>";
echo "Username: " . $_SESSION["username"] . "<br>";
echo "Email: " . $_SESSION["email"] . "<br>";

// Unset all session variables
session_unset();

// Destroy the session
session_destroy();

// Print the session variables again
echo "After destroying session:<br>";
echo "Username: " . $_SESSION["username"] . "<br>";
echo "Email: " . $_SESSION["email"] . "<br>";
?>
Output :
Before destroying session:
Username: john_doe
Email: john_doe@example.com
After destroying session:
Username: 
Email:
In this example, we start the session using the `session_start()` function, set two session variables, and then print their values.

We then unset all session variables using the `session_unset()` function, destroy the session using the `session_destroy()` function, and finally print the session variables again to show that they have been destroyed.