Neil_Bradley
asked on
Destrot session
I have many sesssion variables and I would like to destroy all and keep one. Is this possible?
IE:
Destroy all session variables except $_SESSION['mySpecialSessio n'] ??
Thanks,
N
IE:
Destroy all session variables except $_SESSION['mySpecialSessio
Thanks,
N
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To destroy the session completely you need to use session_destroy(); and to unset the session you need to use session_unset .
1.Since you need keep only $_SESSION['mySpecialSessio n'], u need to unset the all the other session session_unset variables except this one.
2.The other options would to store only this value in $_COOKIES, so that you can destroy the other session values.
1.Since you need keep only $_SESSION['mySpecialSessio
2.The other options would to store only this value in $_COOKIES, so that you can destroy the other session values.
foreach ($_SESSION as $key=>$val)
if ( $key == "mySpecialSession" ) {
echo "Not Deleting mySpecialSession";
} else {
unset ($_SESSION[$key]);
};
This will work:
$temp = $_SESSION['mySpecialSessio n'];
$_SESSION = array();
$_SESSION['mySpecialSessio n'] = $temp;
Beware of the interaction of $_SESSION with register_globals.
http://php.net/manual/en/security.globals.php
Best regards, ~Ray
$temp = $_SESSION['mySpecialSessio
$_SESSION = array();
$_SESSION['mySpecialSessio
Beware of the interaction of $_SESSION with register_globals.
http://php.net/manual/en/security.globals.php
Best regards, ~Ray
Add the following function:
To use it add you special variables in array and pass them to function as the following:
function unset_session($exceptionVars = null)
{
foreach ($_SESSION as $key => $value)
{
if ($exceptionVars != null)
{
//echo array_search($key, $exceptionVars);
if (array_search($key, $exceptionVars) === false)
unset($_SESSION[$key]);
}
}
}
To use it add you special variables in array and pass them to function as the following:
$exceptionVars[0] = "mySpecialSession";
$exceptionVars[1] = "anotherSpecialSessionVar";
unset_session($exceptionVars);
ASKER
Some interesting solution posted but in the end I used the simplest solution. I simply stored the session variable, destroyed the session then re opened it again before re creating my stored session.
If you used session_destroy() to destroy the session you may be in for some unpleasant surprises. "Destroy" is a term of art when using PHP sessions. Just be careful that your solution actually does what you think it will do.
ASKER
Thanks Ray, back in the saddle this morning so will be testing the theory..
N
N
unset($_SESSION['other']);