note 15615 added to function.setcookie

From: Date: Sun, 23 Sep 2001 13:40:33 +0000
Subject: note 15615 added to function.setcookie
Groups: php.notes 
Request: Send a blank email to php-notes+get-18211@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); ?> -- http://www.php.net/manual/en/function.setcookie.php http://master.php.net/manage/user-notes.php?action=edit+15615 http://master.php.net/manage/user-notes.php?action=delete+15615 http://master.php.net/manage/user-notes.php?action=reject+15615

« previous php.notes (#18211) next »