Jump to content

Why $_SESSION[$alert] is unset in OOP Login System


Drew2

Recommended Posts

Hello you wonderful coders you.

I've been staring at this line of code (line 74 highlighted below) for a while in m_template.php, and want to share what it's doing.  

function getAlerts()
    {
        $data = '';
        foreach($this->alertTypes as $alert)
        {            
            if (isset($_SESSION[$alert]))
            {
                foreach($_SESSION[$alert] as $value)
                {
                    $data .= '<li class="'. $alert .'">' . $value . '</li>';
                }
                unset($_SESSION[$alert]);
            }
        }
        return $data;
    }

The unset is here so that the next time a user clicks the submit button, the $_SESSION variable will have a clean slate (at least as far as alerts are concerned).  Otherwise, a user might click the submit button when they've entered valid information, and receive an error message in the members page. 

I just commented out the unset line and ran the code.  First I entered wrong information and clicked submit.  So $_SESSION now has this in it "you entered an invalid username or password."  Then, I entered valid information (so now $_SESSION should also have this in it "You successfully logged in to the members page").  When I did that, I was redirected to the members page.  At the top were the alerts.  There was an alert saying "You successfully logged in to the members page" and then the error: "you entered an invalid username or password."

Just thought others may have been confused like me about why that line of code is in there at all.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...