View Full Version : 4.3.2 and unsetting a session var?

09-08-2003, 08:19 PM
if(is_array($_SESSION) && count($_SESSION) > 0)
foreach($_SESSION AS $var=>$val)
$_SESSION[$var] = '';
$GLOBALS[$var] = '';
global $$var;
$$var = '';
setcookie (session_name(), '', (time () - 2592000), '/', '', 0);

You might be able to guess from that, that I've been trying for a while to either set the value to '' or totally unset the particular session variable.
I have used the above in an auto-redirecting (session-killer) page and in a main page that doesn't redirect.
A page on the site with

if(isset($_SESSION['varname']) && $_SESSION['varname'] != '')
// this gets executed after all the unsetting

has the session var miraculously reappear.

The buglist has a few references, most of which are slammed down as 'bogus' without any of the php developers actually informing how to unset a session variable so that it isn't accessible on the next page.

buglist search (http://bugs.php.net/search.php?search_for=unset+session&boolean=0&limit=10&order_by=&direction=ASC&cmd=display&status=All&php_os=&phpver=&assign=&author_email=&bug_age=0)
4.3.2 bug notice (http://bugs.php.net/bug.php?id=24270)

Any ideas?

09-08-2003, 11:14 PM
A fair few hours down the line (and a few less hairs on the ol' head) and I gave up with using sessions for that bit and moved to using $_GET.

09-09-2003, 01:33 AM
Normally you just delete a variable from a $_SESSION array like this:


I'm still dicyphering your code though - not that easy at all. A few general hints that might point at the source of the confusion:

1. Don't use session_register, session_unregister if you use $_SESSION. There are strange cases where they cause trouble if used in the same script, yours might be one of them.

2. foreach() operates on a copy of the original array.