Search code examples
phpsessionunset

unset a group of PHP sessions


I recently ran into a strange problem with PHP $_SESSION variables.

On renewal-check.php we retreive a user's information from the database and save it like this:

$_SESSION['parent']['mailing_address'];
$_SESSION['parent']['home_phone'];
$_SESSION['parent']['cell_phone'];

They are then asked to enter one of those values to confirm their identity. If they cannot confirm, $_SESSION['parent'] is unset() with unset($_SESSION['parent']);

However, running echo $_SESSION['parent']['somevalue'] still gives data. How do I unset all session variables under $_SESSION['parent'] if unset() isn't working?

Example code:

page1.php

session_start();
$_SESSION['parent']['mailing_address'] = "somevalue";
$_SESSION['parent']['home_phone'] = "somevalue";
$_SESSION['parent']['cell_phone'] = "somevalue";

page2.php

session_start();
unset($_SESSION['parent']);
echo $_SESSION['parent']['cell_phone'];

Page2.php still outputs data, even after $_SESSION['parent'] was unset.


Solution

  • unset($_SESSION['var']) is the correct function to use.

    Have you initialized the session with session_start() ?

    If you have, there might be an issue with your PHP installation.