Jump to content

session unset not working


MDanz

Recommended Posts

Are you even sure that the IF statement is resulting to TRUE? Your code is assuming values are present and you shouldn't use a variable as the condition of a comparison to check if the value is set or has a value.

 

What does the following output?

 

$itemlisted = (isset($_POST['itemlisted'])) ? trim($_POST['itemlisted']) : '';
if (!empty($itemlisted))
{
    echo "Posted value: {$itemlisted}<br />";
   echo "SESSION Before unset: <br />";
    print_r($_SESSION);
    unset($_SESSION['items'][$itemlisted]);
   echo "SESSION After unset: <br />";
    print_r($_SESSION);
}

it prints this

 

Posted value: 1
SESSION Before unset:
Array ( [url] => http://www.eg.com/cart.php [items] => Array ( [4] => 13 [5] => 13 [6] => 13 [7] => 14 ) ) SESSION After unset:
Array ( [url] => http://www.eg.com/cart.php [items] => Array ( [4] => 13 [5] => 13 [6] => 13

 

i think i know... why is the Array not starting from

 [0]

?

does anyone know why the array is not starting from zero.  i get this

 

Posted value: 1
SESSION Before unset:
Array ( [4] => 13 [5] => 13 [6] => 13 [7] => 14 ) SESSION After unset:
Array ( [4] => 13 [5] => 13 [6] => 13 [7] => 14 )

 

 

when i use this code

 

$itemlisted = (isset($_POST['itemlisted'])) ? trim($_POST['itemlisted']) : '';
if (!empty($itemlisted))
{
    echo "Posted value: {$itemlisted}<br />";
   echo "SESSION Before unset: <br />";
    print_r($itemid);
    unset($_SESSION['items'][$itemlisted]);
   echo "SESSION After unset: <br />";
    print_r($itemid);
}

$_SESSION['items'] = array(); will re-initialize it as an empty array. Without knowing more about your application, I don't know if it's appropriate to do so or not, though. You'll have to decide that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.