steveneaston
steveneaston

Reputation: 259

Clear session variable after use

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:

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!

Upvotes: 12

Views: 61196

Answers (6)

donlaur
donlaur

Reputation: 1287

I noticed a small error in the original example that might cause other problems.

unset($_SESSION['showUpdated'];

needs to be

unset($_SESSION['showUpdated']);

Not including that end ) in the unset will cause an error.

Upvotes: 23

tomasbelusky
tomasbelusky

Reputation: 145

Or you can just set it to false.

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

And it looks like you use smaller version of PHP than 5.3, because in 5.3 you'll get notice when you use uninitialized value. So you should use isset function:

if (isset($_SESSION['showUpdated']) && $_SESSION['showUpdated']) {
 echo "Settings Updated";
 $_SESSION['showUpdated'] = false;
}

Upvotes: 0

TheJacobTaylor
TheJacobTaylor

Reputation: 4143

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'])) {

Upvotes: 1

Scott Saunders
Scott Saunders

Reputation: 30394

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.

Upvotes: 3

Jim
Jim

Reputation: 18853

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.

Upvotes: 2

spinon
spinon

Reputation: 10847

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.

Upvotes: 3

Related Questions