Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

session_destroy() runs but the user is still logged in

Errorsession_destroy() is called but $_SESSION values are still readable afterwards

Short 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.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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());
  2. 2Match what you see, then jump to its fix

    1. 1Code later in the same request still sees the user data$_SESSION is not cleared by session_destroy()Fix
    2. 2The PHPSESSID cookie is still listed after logoutThe session cookie is still in the browserFix
    3. 3Nothing changes and no session exists in the logout scriptsession_start() was not called before session_destroy()Fix
    4. 4Logout works only sometimes; an Ajax poll runs at the same timeA parallel request writes the session backFix

What does the error look like?

  • session_destroy() is called but $_SESSION values are still readable afterwards
  • the user is still logged in after the logout page
  • the 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

Did this fix it?

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.php

Confirmed by php.net

Did this fix it?

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.php

2Fix

Call session_start() at the top of the logout script, before any output.

<?php
session_start();
// clear and destroy as above

3Verify

The logout script now ends the session; reload a protected page and you are sent to login.

php -l logout.php

Confirmed by php.net

Did this fix it?

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/ | head

2Fix

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.php

Confirmed by php.net

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.