note 69559 deleted from function.session-destroy by colder

From: Date: Sun, 10 Sep 2006 21:35:24 +0000
Subject: note 69559 deleted from function.session-destroy by colder
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-116989@lists.php.net to get a copy of this message
Note Submitter: webmaster at unitedscripters dot com Reason: bad code ---- The native value of an uninitialized $_SESSION is NULL. <?php $before=@gettype($_SESSION); session_start(); $after=@gettype($_SESSION); print $before.' '.$after.'<br>'; //prints: NULL array ?> It seems therefore more appropriate to unset it via $_SESSION=null; In fact in case your script are checking whether a session exists by isset($_SESSION) which is a legitimate and reasonable check, if you try to unset the session via $_SESSION=array() the check would return true. This would not happen if you unset by $_SESSION=null; <?php session_start(); if(isset($_SESSION)){ print 'A session exists after having used session_start()<br>'; } $_SESSION=array(); if(isset($_SESSION)){ print 'A session exists after having reset it to $_SESSION=array()<br>'; } session_destroy(); if(isset($_SESSION)){ print 'A session exists after having called in session_destroy() AND having set $_SESSION=array()<br>'; } $_SESSION=null; if(isset($_SESSION)){ print 'A session exists<br>'; } else{ print 'A session does NOT exist anymore after having set it to $_SESSION=null'; } ?> That prints: A session exists after having used session_start() A session exists after having reset it to $_SESSION=array() A session exists after having called in session_destroy() AND having set $_SESSION=array() a session does NOT exist anymore after having set it to $_SESSION=null Am I mistaken?

« previous php.notes (#116989) next »