Page 1 of 1

How do you clear Session Variables?

Posted: Thu Feb 10, 2011 6:37 am
by simonmlewis
I have this posted to a file:

Code: Select all

if(isset($_POST['raceid']))
{
    $raceid = $_POST['raceid'];
    $_SESSION['raceid']=$raceid;
} else { $raceid=$_SESSION['raceid'];
}
Once the content of this variable is used, I want to clear it, so that if the page is processed again, without passing anything to it, it won't just run the script.

The query does check that the contents of 5 variables are not NULL.
I have set all of these variables, after the script is run, to null:

Code: Select all

$raceid = NULL;
Yet when I run the page that processes them, it passes through and completes the task, even though I have set them to NULL.

Is there a better way of clearing these session variables?

Re: How do you clear Session Variables?

Posted: Thu Feb 10, 2011 6:44 am
by Eran
The session variable is stored in $_SESSION. Modifying $raceid will have no effect on that. Also, you want to unset it not give it a null value

Code: Select all

unset($_SESSION['raceid']);

Re: How do you clear Session Variables?

Posted: Thu Feb 10, 2011 6:53 am
by simonmlewis
So 'unset' it means the session is ...well..... unset. Gone. Is that right?

Re: How do you clear Session Variables?

Posted: Thu Feb 10, 2011 7:03 am
by Eran
unset() unsets variables. The session is not gone, just the value of the index 'raceid'.

Re: How do you clear Session Variables?

Posted: Thu Feb 10, 2011 7:26 am
by Domsore
Maybe session_destroy(); but this would destroy all sessions.