I use some Session variables. When i logout, function show_number should write 0 number but it doesnt.
logout.php:
<?php
session_start();
session_destroy();
require_once('upper.php');
show_number(); //this function is declared on upper.php
?>
index.php:
<?php
$_SESSION['var'] = 1;
echo "<a href="logout.php">Logout</a>
?>
upper.php:
function show_number() { // shows value of $_SESSION['var'];
if (isset($_SESSION['var']))
echo "1";
else
echo "0";
}
And the problem is: When I click Logout link echo still writes number 1 and i have to reload page to see 0 value.
Cheers
The global $_SESSION
object will not be cleared by session_destroy
:
It does not unset any of the global variables associated with the session, or unset the session cookie.
To clear the session data in your code:
session_start();
session_destroy();
$_SESSION = array();
Or to more thoroughly eradicate all session data (from the same documentation page):
<?php
// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();
// Unset all of the session variables.
$_SESSION = array();
// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$params["path"], $params["domain"],
$params["secure"], $params["httponly"]
);
}
// Finally, destroy the session.
session_destroy();
?>