php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25221 Setcookie set Cookie into %00 instead of erased
Submitted: 2003-08-24 05:26 UTC Modified: 2003-08-24 09:19 UTC
From: henrysim78 at yahoo dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.3.2 OS: Win2000
Private report: No CVE-ID: None
 [2003-08-24 05:26 UTC] henrysim78 at yahoo dot com
Description:
------------
I'm using PHP Ver.4.3.2
I have the same problem too, when use setcookie(<cookiename>), Cookie is
set to %00 instead of erased.  

The problem happen when it hit this syntax :
if(isset($_COOKIE['register'])) {echo 
$_COOKIE['register'];}

Neither isset or !empty function could detect whether it's empty or not.
 It always return true, so it will try to display the value
$_COOKIE['register'] that has been deleted.  It returned error message
:
Notice: Uninitialized string offset: 0 in D:\My Web\register.php on line
186

Thanks in advance.


Reproduce code:
---------------
setcookie("register[one]","Bill");  //Set the cookie
setcookie("register[two]","Jim");

setcookie("register")   //Delete the cookie

if(isset($_COOKIE['register']['one'])) 
  {echo $_COOKIE['register']['one'];
} else 
  {echo "COOKIE Not Exist";
}



Expected result:
----------------
It should display "COOKIE Not Exist" as the cookie has been deleted.  


Actual result:
--------------
It try to display the value of deleted COOKIE.  It generated error :
Notice: Uninitialized string offset: 0 in D:\My Web\register.php on line
186

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-24 08:32 UTC] sniper@php.net
Cookies are set with the name you set them to.
You also need to delete them with the name(s) you set them to. Thus deleting these cookies:

setcookie("register[one]");
setcookie("register[two]");

RTFM: 
"You may also set array cookies by using array notation in 
the cookie name. This has the effect of setting as many 
cookies as you have array elements, but when the cookie is 
received by your script, the values are all placed in an 
array with the cookie's name"


 [2003-08-24 09:00 UTC] henrysim78 at yahoo dot com
Thanks, it did work now.

But a question keep remain, why PHP seem recognize those COOKIEs still exist? Setcookie("register") do eliminate all of the "Register" array inside the COOKIE, as when i checked with print_r ($_COOKIE) it give result :
Array ( [PHPSESSID] => f0cf5ed37c49006512def3576cfb7171 [register] => ) 
It means [register][one], [register][two] are no longer exist so the "isset" function suppose to return "FALSE"?
 [2003-08-24 09:08 UTC] sniper@php.net
You set 3 cookies with that script..(one without value, 'register')

 [2003-08-24 09:19 UTC] henrysim78 at yahoo dot com
Thanks.  I just got the idea.  As i eliminate all of the values/elements inside "Register" Array, "Register" is not longer exist as an ARRAY but rather as a simple VARIABLE.  "Notice: Uninitialized string offset" was displayed because I was tried to access it as an Array.

Once again, Thanks.  Keep up the good work ^_^v
Regards.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 15 11:01:32 2025 UTC