How do you clear Session Variables?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you clear Session Variables?

Post 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?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: How do you clear Session Variables?

Post 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']);
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you clear Session Variables?

Post by simonmlewis »

So 'unset' it means the session is ...well..... unset. Gone. Is that right?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: How do you clear Session Variables?

Post by Eran »

unset() unsets variables. The session is not gone, just the value of the index 'raceid'.
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: How do you clear Session Variables?

Post by Domsore »

Maybe session_destroy(); but this would destroy all sessions.
Post Reply