Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroy session, but keep one variable set

I am using session variables to control logins and page access. I use variables to control different user groups that a user belongs to, so I have quite a few session variables.

I also use a session variable to remember the users last visited page upon refresh.

When the user logs out, I use session_destroy(); to remove all variables. What i would like to do is to maintain the last visited page variable even after the user has logged out.

I think I could do it by using the unset function on every other variable, but there are a lot, and was wondering if there was an easier way?

Thanks Eds

like image 732
Eds Avatar asked Feb 20 '26 08:02

Eds


1 Answers

you can try below code for this,

$Arr_not_destoy_session = array('last_visited_id');

        foreach($_SESSION as $sees_key => $sess_val ){
            if(!in_array($sees_key, $Arr_not_destoy_session)){
                unset($_SESSION[$sees_key]);    
            }   
        }

this will unset all the session variables except 'last_visited_id' only.you can also add more values in this array which you dont want to remove later on..

Thanks.

like image 118
Chandresh M Avatar answered Feb 22 '26 21:02

Chandresh M