views:

34

answers:

5

Is it possible to use a session variable, then unset it directly after?

Example:

//====
//Process Form
if ($_POST['Submit']) {
    $update = $userSettings->update($_POST);

    //If there are form errors
 if (!$update) {
        //Load the errors into an array
        $errors = $update[1];
    } else {
        //Set the session
        $_SESSION['showUpdated'] = true;

        //Redirect to this page
        header("Location: http://www.mysite.com/settings");
    }
}

//==================

if ($_SESSION['showUpdated']) {
 echo "Settings Updated";
    unset($_SESSION['showUpdated'];
}

So after the form is submitted, if there are no errors:

  • Set a session to say the form submission was okay
  • Reload the page (to prevent re-submitted POST data)
  • If the 'showUpdated' session variable is set, display the "Updated" message
  • Unset the session variable (so we don't see the message on next reload)

Currently the problem is, if you unset the session variable straight after; It is as if you have un-set it before the "if exists" part.

Any solutions? Is this even the best way to do it?

Many thanks!

A: 

Just check to see if it exists. This is safe to do before it has been defined and will tell you your answer after it has been defined.

if(!empty($_SESSION['showUpdated'])) {
TheJacobTaylor
Exists is not a function, do you mean isset?
Anders S
Yeah, like that. Can you tell that it has been a little while since I coded in PHP? Thanks @Anders S
TheJacobTaylor
I went with !empty since, if I am not mistaken, it will not throw any warnings, errors, or exceptions if the array or key do not exist.
TheJacobTaylor
If all you care about is if the variable exists or not you should use isset. Empty will return true if the variable is an empty string, 0, NULL, false or an empty array.
Anders S
In this case, there will always be a value, so I am not worried about empty strings, 0, NULL, false, or an empty array. I thought empty would not log a warning if the array key did not exist, while isset will.
TheJacobTaylor
A: 

I do this from time to time. I never have any problems with it. But what I would add to yours is an exit() function call after the header redirect.

EDIT: The reason for the exit() is that it will prevent it from processing any further code and will eliminate the possibility of unset before you wanted to check after the redirect.

spinon
A: 

The header call without an exit after will continue running the page.

header("Location: http://www.mysite.com/settings");
exit;

Using that instead, should kill the page and not unset the session variable on the same page call.

Brad F Jacobs
A: 

That looks like it should work. Make sure you call session_start() before trying to use the session, and always exit() or die() after a redirect header.

I accomplish what you're doing a little differently. I keep a 'message' element in the session. I'll stick text in like 'Your data was saved', error messages, etc. Then, on each page (actually in a page template class), I check to see if the $_SESSION['message'] is set and not empty. If there's something there, I display the message and set the value to an empty string or null.

Scott Saunders
exit() or die() was what I was missing, thanks!
steveneaston
A: 
System