Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Destrot session

Posted on 2011-04-22
9
Medium Priority
?
246 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
0
Comment
Question by:Neil_Bradley
9 Comments
 
LVL 14

Expert Comment

by:Kalpan
ID: 35452091
use the following

unset($_SESSION['other']);
0
 
LVL 8

Accepted Solution

by:
jimsweb earned 2000 total points
ID: 35452096
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
 
LVL 3

Expert Comment

by:pius_babbun
ID: 35452103
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:benhanson
ID: 35452135

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

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35453454
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
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 35453576
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
 
LVL 5

Author Closing Comment

by:Neil_Bradley
ID: 35457689
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35458996
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
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 35462586
Thanks Ray, back in the saddle this morning so will be testing the theory..
N
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question