note 28368 added to function.unset
| From: | radu dot rendec at ines dot ro | Date: | Fri, 10 Jan 2003 19:16:21 +0000 |
| Subject: | note 28368 added to function.unset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-41970@lists.php.net to get a copy of this message | ||
Unset does not work as expected with session variables. Consider the following example:
session_start();
if(isset($_GET["save"])) $_SESSION["x"]=$_GET["save"];
if(isset($_GET["del"])) unset($_SESSION["x"]);
var_dump($_SESSION["x"]);
echo(isset($_SESSION["x"])?"is set":"not set");
Calling the above script with a ?del= parameter (after $_SESSION["x"] was previously set
using ?save=123) gives the expected output. However, if you call the script again, this time with no
parameters, you'll be surprised to find out that $_SESSION["x"] is still there.
I found 2 workarounds for this: one is to assign null instead of calling unset() (i.e.
$_SESSION["x"]=null) -- it's worth takink a look at the note from "nhanasoge at
hotmail dot com" at the beginning of the page.
The second workaround is to use a "host array" as the session variable (which is always
serialized) and unset() keys inside the array.
NOTE: This applies to php <= 4.2.2. I didn't test on 4.2.3 and 4.3.0.
--
http://www.php.net/manual/en/function.unset.php
http://master.php.net/manage/user-notes.php?action=edit+28368
http://master.php.net/manage/user-notes.php?action=delete+28368
http://master.php.net/manage/user-notes.php?action=reject+28368