Why is PHP session_destroy() not working?
I tried to destroy all session variable by using the session_destroy()
method, but after using this method, the values are not destroyed.
Why is session_destroy()
not working?
Is there any other way to destroy the session in PHP?
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800))
{
session_destroy();
session_unset();
}
Perhaps is way too late to respond but, make sure your session is initialized before destroying it.
session_start() ;
session_destroy() ;
i.e. you cannot destroy a session in logout.php if you initialized your session in index.php. You must start the session in logout.php before destroying it.
After using session_destroy()
, the session is destroyed behind the scenes. For some reason this doesn't affect the values in $_SESSION
, which was already populated for this request, but it will be empty in future requests.
You can manually clear $_SESSION
if you so desire ($_SESSION = [];
).
If you need to clear the values of $_SESSION
, set the array equal to an empty array:
$_SESSION = array();
Of course, you can't access the values of $_SESSION
on another page once you call session_destroy
, so it doesn't matter that much.
Try the following:
session_destroy();
$_SESSION = array(); // Clears the $_SESSION variable