I have some session data in a website. I want to destroy all session data when user click another page, except some specific keys like $_SESSION['x']
and $_SESSION['y']
.
Is there a way to do this?
Maybe do something like this
foreach($_SESSION as $key => $val)
{
if ($key !== 'somekey')
{
unset($_SESSION[$key]);
}
}
to unset a particular session variable use.
unset($_SESSION['one']);
to destroy all session variables at one use.
session_destroy()
To free all session variables use.
session_unset();
if you want to destroy all Session variable except x
and y
you can do something like this.
$requiredSessionVar = array('x','y');
foreach($_SESSION as $key => $value) {
if(!in_array($key, $requiredSessionVar)) {
unset($_SESSION[$key]);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With