json_encode / json_decode
$_COOKIE['login'] = json_encode($array); $array = json_decode($_COOKIE['login']);
You can also use serialize / unserialize :
$_COOKIE['login'] = serialize($array); $array = unserialize($_COOKIE['login']);
May be.
UPDATE
With this code:
<html><body><pre><?php $array = Array( 'id' => 1234, 'email' => 'example@example.com', 'token' => base64_encode('abcDEF1234') ); echo "Var Dump (initial):\r\n"; var_dump($array); $serialized = serialize($array); echo "Serialized:\r\n".$serialized."\r\n"; $unserialized = unserialize($serialized); echo "Unserialized:\r\n".$unserailized."\r\n"; var_dump($unserialized); ?></pre></body></html>
You will create the following:
Var Dump (initial): array(3) { ["id"]=> int(1234) ["email"]=> string(19) "example@example.com" ["token"]=> string(16) "YWJjREVGMTIzNA==" } Serialized: a:3:{s:2:"id";i:1234;s:5:"email";s:19:"example@example.com";s:5:"token";s:16:"YWJjREVGMTIzNA==";} Unserialized: array(3) { ["id"]=> int(1234) ["email"]=> string(19) "example@example.com" ["token"]=> string(16) "YWJjREVGMTIzNA==" }
EDIT2
You see an encoded value based on how the HTTP protocol transmits cookies. There are two headers in cookie transfers: Set-Cookie and Cookie . One of them is server-> client, the other is client-> server, with respect.
When PHP sets a cookie (e.g. using setcookie), PHP really just does the following:
setcookie('login',$serialized);
which in PHP translates to:
header('Set-Cookie: login='.urlencode($serialized).'; ' .'expires=Wed, 12-Jan-2011 13:15:00 GMT; ' .'path=/; domain=.mydomain.com');
If you had characters such as : or SPACE, the browser would not know where the cookie properties begin and end.
Brad Christie Jan 12 '11 at 17:56 2011-01-12 17:56
source share