note 28368 rejected from function.unset by didou
| From: | didou@php.net | Date: | Tue, 19 Aug 2003 15:50:11 +0000 |
| Subject: | note 28368 rejected from function.unset by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-54697@lists.php.net to get a copy of this message | ||
Note Submitter: radu.rendec@ines.ro
----
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.