3

ok so I have the following code that echos some session variables that I set already.(They echo like intended.)

(index.php)

<?php session_start();?>
Username: <?php echo $_SESSION['username']; ?><br>
Password(encrypted): <?php echo $_SESSION['password']; ?><br>
ThemeColor: <?php echo $_SESSION['themecolor']; ?><br>


And I have this code which I try to end the session with, but when I run the code above, the variables still echo out, so the session is still active

(logout.php)

<?php
session_start();
session_unset();
session_destroy();
?>


Can you tell me what I'm doing wrong?
Veloncia
  • 117
  • 3
  • 13

4 Answers4

2

Straight from the PHP documentation:

<?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();
?>
Josh Harrison
  • 5,927
  • 1
  • 30
  • 44
  • 1
    Linking to and including a snippet from the documentation is more helpful than an isolated snippet from the documentation, in my opinion. I could just post a link on its own but that is not good SO practice - self-contained answers are. A bit of an unfair downvote. – Josh Harrison Jan 03 '14 at 14:29
1

Keep your existing snippet and try adding the following snippet:

// Clear all values of the $_SESSION array by creating a new one
$_SESSION = array();

// If your session is setup to use cookies, expire the cookie
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

The above snippet was borrowed from it's original answer here. All credit goes to the original author Pekka.

Community
  • 1
  • 1
War10ck
  • 12,387
  • 7
  • 41
  • 54
0

A session use cookie, so you need to destroy this cookie:

    function destroySession()
    {
        $params = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000,
            $params['path'], $params['domain'],
            $params['secure'], $params['httponly']
        );
        session_destroy();
        unset($_SESSION);
    }
ImmortalPC
  • 1,650
  • 1
  • 13
  • 17
0

After using session_destroy(), the session cookie is removed and the session is no longer stored on the server. The values in $_SESSION may still be available, but they will not be on the next page load.

bthall
  • 56
  • 4
  • This is correct apart from the bit about the cookie. From the session_destroy docs: `It does not unset any of the global variables associated with the session, or unset the session cookie.` – Jim Jan 03 '14 at 15:01