We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Destrot session

Neil_Bradley
Neil_Bradley asked
on
Medium Priority
256 Views
Last Modified: 2012-05-11
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['mySpecialSession'] ??
Thanks,
N
Comment
Watch Question

CERTIFIED EXPERT

Commented:
use the following

unset($_SESSION['other']);
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
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['mySpecialSession'], 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.
CERTIFIED EXPERT

Commented:

    foreach ($_SESSION as $key=>$val)
    if ( $key == "mySpecialSession" ) {
      echo "Not Deleting mySpecialSession";
    } else {
        unset ($_SESSION[$key]);
    };

Most Valuable Expert 2011
Author of the Year 2014

Commented:
This will work:

$temp = $_SESSION['mySpecialSession'];
$_SESSION = array();
$_SESSION['mySpecialSession'] = $temp;

Beware of the interaction of $_SESSION with register_globals.
http://php.net/manual/en/security.globals.php

Best regards, ~Ray
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
Add the following function:
 
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]);
		}
	}
}

Open in new window


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);

Open in new window

Neil_BradleyWeb UX/UI Developer

Author

Commented:
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.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
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.
Neil_BradleyWeb UX/UI Developer

Author

Commented:
Thanks Ray, back in the saddle this morning so will be testing the theory..
N
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.