PHP Unset Session Variable

PHP Unset Session Variable

You can unset session variable using:

  1. session_unset - Frees all session variables (It is equal to using: $_SESSION = array(); for older deprecated code)
  2. unset($_SESSION['Products']); - Unset only Products index in session variable. (Remember: You have to use like a function, not as you used)
  3. 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



Leave a reply



Submit