Anon-e-mouse Posted February 18, 2012 Share Posted February 18, 2012 Afternoon, I've hit a brick wall, and I'd like someone to point out where I'm going wrong here as I can't for the life of me think of whats amiss. As it is, I'm trying to set a message to the user in session form, so the function is called passing the error ID. As you can see below, it finds it and assigns it to the private variable within the class. <?php public function set($name){ // Check if the message is available... if(array_key_exists($name, $this->errors)){ // Set it. $_SESSION['cM'] = $this->errors[$name][0]; } else { $_SESSION['cM'] = 'TEST'; } } ?> To then return the message, the function is simply called using the below function which should return the error? Now, when I don't unset the session in anyway shape or form the message is displayed but when I do unset it, no message appears. I was under the impression that as I had already assigned the contents of the session to the $message variable that the session itself was then useless and as such I could then unset it for later use. But apparently not? <?php public function get(){ // Is something set? if(isset($_SESSION['cM'])){ // Yes, into a variable! $message = $_SESSION['cM']; // Unset the session for later use. //unset($GLOBALS['cM']); - Doesnt make any difference. //$_SESSION['cM'] = false; - Doesnt make any difference. //unset($_SESSION['cM']); - Doesnt make any difference. // Return the message. $f = "<p style=\"color:green\">"; $f.= $message; $f.= "</p>"; return $f; } // No, Return false then to stop an error appearing. return false; } ?> As you can see I have tried unsetting the global variable, just session itself and clearing the session but to no avail. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/257236-session-unset/ Share on other sites More sharing options...
jcbones Posted February 18, 2012 Share Posted February 18, 2012 Yes, you can, but you also realize that you will not see that the session is unset until the next page load, as you already have the contents. Try this test. <?php //syntax highlighting public function get(){ // Is something set? if(isset($_SESSION['currentMessage'])){ // Yes, into a variable! $message = $_SESSION['currentMessage']; // Unset the session for later use. unset($_SESSION['currentMessage']); // Return the message. $f = "<p style=\"color:green\">"; $f.= $message; $f.= "</p>"; $f .= $_SESSION['currentMessage']; //You should get an undefined index notice, because currentMessage is not set. return $f; } // No, Return false then to stop an error appearing. return false; } Quote Link to comment https://forums.phpfreaks.com/topic/257236-session-unset/#findComment-1318566 Share on other sites More sharing options...
Anon-e-mouse Posted February 18, 2012 Author Share Posted February 18, 2012 I've tried that but its come back with nothing. It's worth noting that this is called from another class function which is called in the first place. Basically the login function works away and is told to set either a success or a failure message through the registry. Then redirect to either the login page again or the member area where the template file prints the message regardless. Quote Link to comment https://forums.phpfreaks.com/topic/257236-session-unset/#findComment-1318569 Share on other sites More sharing options...
Anon-e-mouse Posted February 18, 2012 Author Share Posted February 18, 2012 After a bit of jiggery pokery I have managed to get it half working. So when a user logs out the message is correctly displayed.. however when a user logs in nothing is shown. To check I dumped the contents of the $_SESSION['cM'] which returns as not even being set with the unidentified index error. However what I can't work out is why it isn't being set correctly as the line in the logout and login function is exactly the same (to test). <?php /* * Logout ... */ // Success. Registry::get("MessageHandler")->set("test"); // Redirect. header("Location: ".HOME); // Done. return; /* * Login ... */ // Success. Registry::get("MessageHandler")->set("test"); // Redirect. header("Location: ".HOME."member-area"); // Done. return; ?> For some reason the logout works but the login doesn't. Quote Link to comment https://forums.phpfreaks.com/topic/257236-session-unset/#findComment-1318705 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.