• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

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['mySpecialSession'] ??
Thanks,
N
0
Neil_Bradley
Asked:
Neil_Bradley
1 Solution
 
KalpanCommented:
use the following

unset($_SESSION['other']);
0
 
jimswebCommented:
I think, the best way is to store it somewhere before resetting. Once you are done with resetting you can easily restore it back to the original variable.
In this case reset everything except keep a copy of $_SESSION['mySpecialSession']
0
 
pius_babbunCommented:
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.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
benhansonCommented:

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

0
 
Ray PaseurCommented:
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
0
 
Mohamed AbowardaSoftware EngineerCommented:
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

0
 
Neil_BradleyWeb UX/UI DeveloperAuthor 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.
0
 
Ray PaseurCommented:
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.
0
 
Neil_BradleyWeb UX/UI DeveloperAuthor Commented:
Thanks Ray, back in the saddle this morning so will be testing the theory..
N
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now