Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/257236-session-unset/
Share on other sites

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;
        
} 

Link to comment
https://forums.phpfreaks.com/topic/257236-session-unset/#findComment-1318566
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257236-session-unset/#findComment-1318569
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/257236-session-unset/#findComment-1318705
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.