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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.