note 15615 deleted from function.setcookie by sniper
| From: | sniper@php.net | Date: | Wed, 11 Dec 2002 07:20:15 +0000 |
| Subject: | note 15615 deleted from function.setcookie by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-40742@lists.php.net to get a copy of this message | ||
You cannot store arrays/objects in a cookie. To come around that limitation, you need to call
'serialize' before you store the cookie. However, the value of the cookie is sent
serialized back to the client, which means you also need to call 'unserialize' on the
value you get back.
An example:
You need to store this array:
<?php
$user = array(
'Name' => 'JohnDoe'
, 'Pass' => 'FooBar'
);
$user_serialized = serialize($user);
setcookie('user_cookie', $user_serialized);
?>
On another page/file, when you get the cookie, you do:
<?php
if (0 != count($user_cookie)){
// Cookie with user details present
$user = unserialize($user);
}
var_dump($user);
?>