doubledee Posted August 23, 2011 Share Posted August 23, 2011 I am debugging my Log-In module and would like to erase data that was stored in my $_SESSION variable from my prior debugging session. (It will just be easier for me to follow what is going on if all variables in my SESSION are null.) How do I do that? Where do I do that? Do I destroy the SESSION and then start it? Or do I start the SESSION and then destroy it? Debbie Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/ Share on other sites More sharing options...
gizmola Posted August 23, 2011 Share Posted August 23, 2011 I'd suggest a simple function: function cleanSession() { foreach($_SESSION as $sessvar) { unset($sessvar); } } Add it where you want to insure that there are no set session variables. Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261035 Share on other sites More sharing options...
doubledee Posted August 23, 2011 Author Share Posted August 23, 2011 I'd suggest a simple function: function cleanSession() { foreach($_SESSION as $sessvar) { unset($sessvar); } } Add it where you want to insure that there are no set session variables. But I call that function after I start the session in my header? Debbie Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261038 Share on other sites More sharing options...
gizmola Posted August 23, 2011 Share Posted August 23, 2011 Yes. Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261040 Share on other sites More sharing options...
doubledee Posted August 23, 2011 Author Share Posted August 23, 2011 Yes. Why not just destroy the session? Debbie Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261043 Share on other sites More sharing options...
gizmola Posted August 23, 2011 Share Posted August 23, 2011 Because then you would have to reinitialize the session. You stated you wanted to have something you could drop in for testing purposes that doesn't do black magic with the session handler. If you really want to use session_destroy then you'd have to have: session_start(); session_destroy(); session_start(); Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261046 Share on other sites More sharing options...
doubledee Posted August 23, 2011 Author Share Posted August 23, 2011 Because then you would have to reinitialize the session. You stated you wanted to have something you could drop in for testing purposes that doesn't do black magic with the session handler. If you really want to use session_destroy then you'd have to have: session_start(); session_destroy(); session_start(); Oh, okay. Debbie Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261052 Share on other sites More sharing options...
codefossa Posted August 23, 2011 Share Posted August 23, 2011 Well, it wouldn't be that bad lookin'. $session_id = session_id(); if (empty($session_id)) session_start(); if (isset($_GET['logout'])) { session_destroy(); session_start(); } The session_start func should be there no matter what. The logout may have more to it as well (assuming that's why you're destroying it), but same thing pretty much no matter what. Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261054 Share on other sites More sharing options...
doubledee Posted August 23, 2011 Author Share Posted August 23, 2011 I'd suggest a simple function: function cleanSession() { foreach($_SESSION as $sessvar) { unset($sessvar); } } By the way, I added your code in like... <?php // Initialize a session. session_start(); foreach($_SESSION as $sessvar) { unset($sessvar); } But when I step through NetBeans it isn't re-setting my SESSION variables?! Debbie Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261055 Share on other sites More sharing options...
darkfreaks Posted August 23, 2011 Share Posted August 23, 2011 i am going to download netbeans on Ubuntu and see why Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261077 Share on other sites More sharing options...
requinix Posted August 23, 2011 Share Posted August 23, 2011 I think gizmola was aiming more for foreach(array_keys($_SESSION) as $sessvar) { unset($_SESSION[$sessvar]); } Right now it'll just unset $sessvar and that won't affect what's in $_SESSION. Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261095 Share on other sites More sharing options...
gizmola Posted August 23, 2011 Share Posted August 23, 2011 Yes my bad -- thank requinix. Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261116 Share on other sites More sharing options...
doubledee Posted August 24, 2011 Author Share Posted August 24, 2011 I think gizmola was aiming more for foreach(array_keys($_SESSION) as $sessvar) { unset($_SESSION[$sessvar]); } Right now it'll just unset $sessvar and that won't affect what's in $_SESSION. I don't get it?! I thought "un-setting" was like erasing each attribute? So what should it be to erase each value? Debbie Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261136 Share on other sites More sharing options...
requinix Posted August 24, 2011 Share Posted August 24, 2011 It should be what I posted. unset() will destroy values in memory according to what variable you give it. If you $a = 1; $b = $a; unset($b); then you are destroying $b, and that has no bearing on what happens to $a. Similarly foreach ($array as $value) { unset($value); } will destroy $value, and that has no bearing on what happens to $array. By using array keys like foreach (array_keys($array) as $key) { unset($array[$key]); } then you are destroying $array[$key]. Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261449 Share on other sites More sharing options...
doubledee Posted August 24, 2011 Author Share Posted August 24, 2011 It should be what I posted. unset() will destroy values in memory according to what variable you give it. If you $a = 1; $b = $a; unset($b); then you are destroying $b, and that has no bearing on what happens to $a. Similarly foreach ($array as $value) { unset($value); } will destroy $value, and that has no bearing on what happens to $array. By using array keys like foreach (array_keys($array) as $key) { unset($array[$key]); } then you are destroying $array[$key]. But even if I use your original code and just "unset" the Session values, should I see the values go to '' or null in NetBeans as the code runs through the loop? Debbie Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261534 Share on other sites More sharing options...
requinix Posted August 24, 2011 Share Posted August 24, 2011 I don't know what NetBeans will do. I would expect null because the variables/values no longer exist. Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261565 Share on other sites More sharing options...
gizmola Posted August 24, 2011 Share Posted August 24, 2011 Yeah not sure about netbeans debugger -- things should go null on an unset(), but even if they don't you could verify easily that they are gone using vardump. This is the entire purpose of unset(). Link to comment https://forums.phpfreaks.com/topic/245519-initialze-a-session/#findComment-1261572 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.