You can unset session variable using:
session_unset- Frees all session variables (It is equal to using:$_SESSION = array();for older deprecated code)unset($_SESSION['Products']);- Unset only Products index in session variable. (Remember: You have to use like a function, not as you used)session_destroyโ Destroys all data registered to a session
To know the difference between using session_unset and session_destroy, read this SO answer. That helps.
You can unset session variable using:
session_unset- Frees all session variables (It is equal to using:$_SESSION = array();for older deprecated code)unset($_SESSION['Products']);- Unset only Products index in session variable. (Remember: You have to use like a function, not as you used)session_destroyโ Destroys all data registered to a session
To know the difference between using session_unset and session_destroy, read this SO answer. That helps.
I am including this answer in case someone comes to this page for the same reason I did. I just wasted an embarrassing amount of time trying to track the problem down. I was calling:
unset($_SESSION['myVar']);
from a logout script. Then navigating to a page that required login, and the server still thought I was logged in. The problem was that the logout script was not calling:
session_start();
Unsetting a session var DOES NOT WORK unless you start the session first.
Videos
Unset will destroy a particular session variable whereas session_destroy() will destroy all the session data for that user.
It really depends on your application as to which one you should use. Just keep the above in mind.
unset($_SESSION['name']); // will delete just the name data
session_destroy(); // will delete ALL data associated with that user.
Something to be aware of, the $_SESSION variables are still set in the same page after calling session_destroy() where as this is not the case when using unset($_SESSION) or $_SESSION = array(). Also, unset($_SESSION) blows away the $_SESSION superglobal so only do this when you're destroying a session.
With all that said, it's best to do like the PHP docs has it in the first example for session_destroy().
Please check your code is not right
Your wrong code
if ($curLocation='london')
Correct
if ($curLocation=='london')
To destroy a session all you need to do is:
<?php
session_start();
session_destroy();
?>
And you don't need to unset anything as you're destroying it anyway.
However if you want to only unset a specific variable, you do that with:
unset($_SESSION['variableName']);
Also, This code:
$curLocation = $_SESSION['curLocation'];
if($curLocation=='')
{
$curLocation='london';
}
can be written as:
if (! isset($_SESSION['curLocation']) ) {
$_SESSION['curLocation'] = 'london';
}
What about
unset($_SESSION["products"])
instead of the
session_destroy()
There is only one session per user. So there is no way to destroy a "specific" session. What you can do is delete the contents of your session responsible for the display of the cart (as shown above).
Use,
unset($_SESSION["products"]);
session_destroy() will destroy all the sessions, while the above line would destroy a specific session variable.
Use unset() for all the session variables specific to either site 1 or 2.
unset($_SESSION['site1']);
//or
unset($_SESSION['site2']);
Just so that you know, session_is_registered is deprecated as of PHP version 5.3.0. See docs.
Before unset($_SESSION['site1']); put session_start() like this
<?php
session_start();
unset($_SESSION['site1']);
?>
session_unset just clears the $_SESSION variable. Itโs equivalent to doing:
$_SESSION = array();
So this does only affect the local $_SESSION variable instance but not the session data in the session storage.
In contrast to that, session_destroy destroys the session data that is stored in the session storage (e.g. the session file in the file system).
Everything else remains unchanged.
session_destroy(); is deleting the whole session.
session_unset(); deletes only the variables from session - session still exists. Only data is truncated.