0

I'm pretty confused about session management. I'm using PHP, and when the user clicks the log in button, I call session_start() as the first line in my script. The result of that is a cookie on the client called PHPSESSID with a unique value. That seems to work as I would expect.

Where I am confused is on the log out process. When the user clicks 'log out', I call session_unset(); and then session_destroy();. My expectation is that this should clear the cookie, but doesn't. Even when I close the tab in the browser, when I come back to the site via a new tab, that old cookie is still there, along with the old session id value. This means that the session id is being reused from one session to the next (and beyond). Even when I go through the log in process again, the session id remains unchanged. This can't be correct, right?

In a somewhat desperate attempt, I tried to use this function to clear the cookie itself, but it seems to accomplish nothing:

// destroy the cookies
var cookies = document.cookie.split(";");

for (var i = 0; i < cookies.length; i++) {
    var cookie = cookies[i];
    var eqPos = cookie.indexOf("=");
    var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}

Am I just misunderstanding how this process works? Or am I doing something wrong (or both!). Thanks for any advice or references.

AndroidDev
  • 20,466
  • 42
  • 148
  • 239

3 Answers3

1

You also need to unset the session variables and kill the cookies:

$_SESSION = array();

if (isset($_COOKIE[session_name()])) { 
    $params = session_get_cookie_params();
    setcookie(session_name(), '', 1, $params['path'], $params['domain'],
             $params['secure'], isset($params['httponly']));
}

This question has been answered extensively here.

Community
  • 1
  • 1
phpPhil
  • 906
  • 1
  • 9
  • 28
1

it may Help you

session_start();
if (isset($_COOKIE['remember_user'])) {
unset($_COOKIE['Hello']);
unset($_COOKIE['HelloTest1']);
setcookie('Hello', null, -1, '/');
setcookie('HelloTest1', null, -1, '/');
return true;
} else {
return false;
}
session_destroy();
header('Location:');
exit;
priya786
  • 1,804
  • 12
  • 11
0

You have to start the session (session_start) in logout page also.. Did you do it ?

phpfresher
  • 310
  • 1
  • 12