jamesjmann Posted March 16, 2011 Share Posted March 16, 2011 I already know how to empty individual $_SESSION[""] array elements, but I was wondering how you could empty ALL $_SESSION array elements without having to write out unset ($_SESSION[""]); for each one? Is it just unset ($_SESSION);? Sorry if this seems like a stupid question, but I'm just trying to make sure, because I have a lot of $_SESSION variables and am trying to simplify the task of emptying them all. Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/ Share on other sites More sharing options...
AbraCadaver Posted March 16, 2011 Share Posted March 16, 2011 Yes, have you tried it? unset ($_SESSION); //or to empty it $_SESSION = array(); Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188277 Share on other sites More sharing options...
jamesjmann Posted March 16, 2011 Author Share Posted March 16, 2011 Yes, have you tried it? unset ($_SESSION); //or to empty it $_SESSION = array(); No. Is there really difference between the two methods you posted? Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188443 Share on other sites More sharing options...
nicholasolsen Posted March 16, 2011 Share Posted March 16, 2011 session_unset(); session_destroy(); .................. google next time Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188448 Share on other sites More sharing options...
jamesjmann Posted March 16, 2011 Author Share Posted March 16, 2011 session_unset(); session_destroy(); .................. google next time I'm pretty sure phpfreaks.com was meant for questions like this. Why would I sign up on phpfreaks if I could just google all the answers to my questions? Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188452 Share on other sites More sharing options...
nicholasolsen Posted March 16, 2011 Share Posted March 16, 2011 session_unset(); session_destroy(); .................. google next time I'm pretty sure phpfreaks.com was meant for questions like this. Why would I sign up on phpfreaks if I could just google all the answers to my questions? Sorry if you took that little comment to harsh, but when i google unset sessions, PHP.net is the first hit with the correct answer. PHP.net has basically all the solutions to problems like this, because they are so basic and fundamental. Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188455 Share on other sites More sharing options...
btherl Posted March 17, 2011 Share Posted March 17, 2011 The "official" example code for finishing a session is here: http://www.php.net/manual/en/function.session-destroy.php session_unset() is deprecated. Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188459 Share on other sites More sharing options...
Pikachu2000 Posted March 17, 2011 Share Posted March 17, 2011 session_unset(); session_destroy(); .................. google next time I'm pretty sure phpfreaks.com was meant for questions like this. Why would I sign up on phpfreaks if I could just google all the answers to my questions? It's actually part of the forum rules. Users will attempt to find answers to their questions by the following before posting: 1. Search PHP Freaks Forum for the answer. Chances are this question has been asked before. 2. Search the PHP Manual for a function and or information about a function to fix the error or achieve the desired results. 3. Search Google for an answer to their question. As stated, it has probably been asked and solved many times before. Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188472 Share on other sites More sharing options...
jamesjmann Posted March 17, 2011 Author Share Posted March 17, 2011 Thanks, btherl. @pikachu2000 Thanks for the heads up, but I don't think it's up to nicholasolsen to point out my mistakes. I believe only moderator's are allowed to correct a member, or am I wrong? Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188480 Share on other sites More sharing options...
btherl Posted March 17, 2011 Share Posted March 17, 2011 I'm not aware of any rule that only moderators can correct members. There's also no rule that members can tell other members what to do. So I would say common sense and politeness should take priority here. Especially since he gave you the wrong answer, it would have been better if he said "I found this on google" rather than "google next time". Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188486 Share on other sites More sharing options...
jamesjmann Posted March 17, 2011 Author Share Posted March 17, 2011 I'm not aware of any rule that only moderators can correct members. There's also no rule that members can tell other members what to do. So I would say common sense and politeness should take priority here. Especially since he gave you the wrong answer, it would have been better if he said "I found this on google" rather than "google next time". It should be a rule. Its very rude to make comments like that, and I'm not going to lie, he made me feel stupid lol, which p***ed me off. Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188487 Share on other sites More sharing options...
nicholasolsen Posted March 17, 2011 Share Posted March 17, 2011 This is all off topic so drop it. ... Especially since he gave you the wrong answer, it would have been better if he said "I found this on google" rather than "google next time". I wouldnt say the answer i gave was wrong.. The q was how to unset all sessions, and the two functions i listed does exactly that, am i right? So go somewhere else being a smartypant, Mr. Smartypants :-) :-) Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188492 Share on other sites More sharing options...
btherl Posted March 17, 2011 Share Posted March 17, 2011 nicholasolsen, please see the following on the manual page for session_unset(): If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used, use unset() to unregister a session variable, i.e. unset ($_SESSION['varname']);. As the OP was using $_SESSION (and most PHP installations these days do), session_unset() is not the correct function to use. Also, the session_destroy() manual page says that session_unset() is deprecated, and specifically instructs to use it only for code that does not use $_SESSION. Edit: I would also like to point out that these are the things you won't find easily by using google. Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188497 Share on other sites More sharing options...
nicholasolsen Posted March 17, 2011 Share Posted March 17, 2011 nicholasolsen, please see the following on the manual page for session_unset(): If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used, use unset() to unregister a session variable, i.e. unset ($_SESSION['varname']);. As the OP was using $_SESSION (and most PHP installations these days do), session_unset() is not the correct function to use. Also, the session_destroy() manual page says that session_unset() is deprecated, and specifically instructs to use it only for code that does not use $_SESSION. Edit: I would also like to point out that these are the things you won't find easily by using google. However, it does the trick. Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188501 Share on other sites More sharing options...
jamesjmann Posted March 17, 2011 Author Share Posted March 17, 2011 However, it does the trick. Yes, but that function is now deprecated, and from what I've read, $_SESSION = array (); is the best way to go. Also, I've heard there are bugs when using the unset function in webkit browsers... Quote Link to comment https://forums.phpfreaks.com/topic/230820-emptying-_session-arrays/#findComment-1188517 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.