using array into cookie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mianmajidali
Forum Commoner
Posts: 30
Joined: Tue Dec 01, 2009 8:05 pm

using array into cookie

Post 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
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: using array into cookie

Post 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
Post Reply