Page 1 of 1

using array into cookie

Posted: Mon Aug 16, 2010 6:27 am
by mianmajidali
hi to all,
i am using array into cookie, code is bellow
$data=array("apple", "banana", "orange", "mango");
$final=serialize($data);
$exp=time()+3600;
setcookie('fav_names', $final, $exp);
foreach(unserialize($_COOKIE['fav_names']) as $k => $v)
echo $k . '----' . $v ;


This is the Error:
Warning: Invalid argument supplied for foreach() in D:\VertrigoServ\www\names-meanings\helper.php on line 6

Re: using array into cookie

Posted: Mon Aug 16, 2010 8:25 am
by shawngoldw
I could be mistaken but I don't think $_COOKIE[] will contain what you put in setcookie() until the next page load. Again, I may be mistaken but I think $_COOKIE[] gets populated when the php script starts up, setcookie() sends header information, and on subsequent page loads, the browser sends those cookies back to the server.

Shawn