PHP Unset Session Variable
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.
Unset session in php
Use two =
and not one
<?php
session_destroy();
if ($curLocation == 'london') //here
{
unset($_SESSION['curLocation']);
}
?>
What you are doing is assigning a value to $curLocation
instead of comparing it to something
How do I destroy a specific session variable in PHP?
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).
PHP - Unset $_SESSION variables using foreach
You must get the key and unset
$_SESSION[$key]
or pass your variable by reference (not sure if you can unset
a reference).
foreach($_SESSION as $key => $value) {
unset($_SESSION[$key]);
}
foreach($_SESSION as &$value) {
unset($value);
}
Related Topics
Select MySQL Based Only on Month and Year
How to Load View from Alternative Directory in Laravel 4
HTML into PHP Variable (HTML Outside PHP Code)
Is This the Correct Way to Send Email with PHP
How to Retrieve All Variables from a Twig Template
PHP Exec() Is Not Executing the Command
PHP Allocate Color Without Image Resource
Need Guidance to Start with Zend Acl
Why Is It Good Save to Save Sessions in the Database
Convert a JSON into a Utf-8 String
Jetbrains Webide: PHP Variable Type Hinting
Check for Session Timeout in Laravel
Using Preg_Split with Multiple Spaces
Zf2 + Doctrine 2 - Child-Level Discriminators with Class Table Inheritance