session_destroy() runs but the user is still logged in
session_destroy() is called but $_SESSION values are still readable afterwardsShort answer
session_destroy() removes the stored session data, but it does not empty $_SESSION in the running script and does not delete the session cookie. Call session_start() first, set $_SESSION to an empty array, expire the cookie, then call session_destroy(). Check for parallel requests that rewrite the session.
Fix it fast
1Find out which cause you have
Print what the logout script sees right after the call. If $_SESSION still has values, you hit the first cause.
<?php session_start(); session_destroy(); var_dump($_SESSION, session_id());2Match what you see, then jump to its fix
- 1
Code later in the same request still sees the user data$_SESSION is not cleared by session_destroy()Fix - 2
The PHPSESSID cookie is still listed after logoutThe session cookie is still in the browserFix - 3
Nothing changes and no session exists in the logout scriptsession_start() was not called before session_destroy()Fix - 4
Logout works only sometimes; an Ajax poll runs at the same timeA parallel request writes the session backFix
- 1
What does the error look like?
session_destroy() is called but $_SESSION values are still readable afterwardsthe user is still logged in after the logout pagethe PHPSESSID cookie is still in the browser after logout
Cause 1 of 4
$_SESSION is not cleared by session_destroy()
1Check
The manual says session_destroy() does not unset any of the global variables associated with the session. The array stays in memory until the script ends.
grep -rn "session_destroy" --include=*.php .2Fix
Empty the array yourself before or after destroying. The manual recommends clearing $_SESSION rather than relying on destroy alone.
<?php
session_start();
$_SESSION = [];
session_destroy();3Verify
Dump $_SESSION after the call; it prints an empty array.
php -r 'session_start(); $_SESSION["u"]=1; $_SESSION=[]; session_destroy(); var_dump($_SESSION);'Confirmed by php.net
Cause 2 of 4
The session cookie is still in the browser
1Check
The session ID must also be unset. With the default cookie propagation, the manual says the cookie must be deleted, and setcookie() can do it.
php -r 'session_start(); var_dump(session_name(), session_get_cookie_params());'2Fix
Expire the cookie with the same parameters it was created with.
<?php
session_start();
$_SESSION = [];
if (ini_get('session.use_cookies')) {
$p = session_get_cookie_params();
setcookie(session_name(), '', time() - 42000,
$p['path'], $p['domain'], $p['secure'], $p['httponly']);
}
session_destroy();3Verify
Open the browser's cookie list after the logout request; the session cookie is gone. The script itself must pass a syntax check.
php -l logout.phpConfirmed by php.net
Cause 3 of 4
session_start() was not called before session_destroy()
1Check
The destroy example in the manual starts the session first. Without an active session the call has nothing to act on.
grep -n "session_start\|session_destroy" logout.php2Fix
Call session_start() at the top of the logout script, before any output.
<?php
session_start();
// clear and destroy as above3Verify
The logout script now ends the session; reload a protected page and you are sent to login.
php -l logout.phpConfirmed by php.net
Cause 4 of 4
A parallel request writes the session back
1Check
The manual warns that immediate deletion can show other concurrent requests sudden data loss, and suggests marking the session with a deletion time instead.
grep -rn "setInterval\|fetch(" public/ | head2Fix
Store a logout timestamp in $_SESSION, reject any request after it, and delete the session in a controlled way, or make sure no concurrent requests run at logout.
<?php
// on logout
$_SESSION['destroyed_at'] = time();
// on every request
if (isset($_SESSION['destroyed_at'])) { http_response_code(401); exit; }3Verify
Log out while the polling page is open; the next poll is rejected.
php -l logout.phpConfirmed by php.net
How do I stop it coming back?
- Put logout in one function that clears $_SESSION, expires the cookie and destroys the session.
- Enable session.use_strict_mode, which the manual recommends for all sites.
- Test logout with the back button and a second tab.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
PHP $_SESSION values are empty on the next page
session_start() has no effect and no cookie is sent
A session works only when the browser sends back the session cookie and session_start() runs before any output.
4 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack end
Apache returns 403 when a URL contains %3F
a request like /page%3Fname=test is refused while /page?name=test works
Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.
2 causes, each with a fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix