Jump to content

resetting session


aebstract

Recommended Posts

I'm using this code to completely clear all sessions and reset the session id back to what it was. This way whoever is logged in will remain logged in and all sessions that were set while they have been logged in, will be cleared. Is there a better way of doing this? It isn't keeping the user logged in like it is supposed to.

 

 


$sessionid = $_SESSION["id"];

unset ($_SESSION);
session_destroy();

$_SESSION["id"] = $sessionid;

Link to comment
https://forums.phpfreaks.com/topic/95485-resetting-session/
Share on other sites

Hmm, I'm not 100% sure, but I'm thinking that destroying the session is causing the problem. Try removing just that line, or you can try this (might be less efficient, but should do what you are looking for):

 

<?php
//Unset all SESSION values except 'id'
foreach ($_SESSION as $index => $value) {
    if ($index!='id') {
        unset($_SESSION[$index]);
    }
}

?>

 

EDIT: Found this in the manual (emphasis added):

 

If you are creating a new session, but want to make sure that  there are currently no sessions active by doing session_destroy(); make sure you start the session again using session_start(); or else your session data will not register properly.
Link to comment
https://forums.phpfreaks.com/topic/95485-resetting-session/#findComment-488803
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.