PHP how to string an array and store in cookie

I have an array like this

$ value = {array ('id' => $ id, 'email' => $ email, 'token' => $ token)}

I want the array string to then encode and then store it in the "login" cookie. How do you do that? Please also tell me how to decode and read the stored value.

Edit:

I am trying to serialize / unserialize, but this did not work as expected. eg,

$value = serialize(array('id'=>33, 'email'=>'big@gmail.com', 'token'=>'e9aa0966773d68e0fbf9cb21fc2877b4')); echo $value; //a:3:{s:2:"id";i:33;s:5:"email";s:20:"big@gmail.com";s:5:"token";s:32:"e9aa0966773d68e0fbf9cb21fc2877b4";} 

But when the value goes into the cookie, it looks like

 a%3A3%3A%7Bs%3A2%3A%22id%22%3Bs%3A1%3A%226%22%3Bs%3A5%3A%22email%22%3Bs%3A20%3A%22craigcosmo%40gmail.com%22%3Bs%3A5%3A%22token%22%3Bs%3A32%3A%22e9aa0966773d68e0fbf9cb21fc2877b4%22%3B%7D 
+6
php
Jan 12 '11 at 17:53
source share
2 answers

there is serialize / unserialize to convert the array to string and vice versa.

Edit: When you store the string in a cookie ( setcookie ), php needs to do url encoding in the string. This prevents any characters in the string stored in the cookie from interfering with other headers. When the page loads next, php receives a cookie and automatically decrypts the url to a cookie value to return it to its previous value. As for what is stored in the cookie, this should not matter in php, because php will automatically encode / decode the url. Now, if you get the cookie in another language, such as javascript, then yes, you will get the original string. In this case, you can use something like decodeURI in JS to return the original value.

+10
Jan 12 '11 at 17:55
source share

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.

+11
Jan 12 '11 at 17:56
source share



All Articles