I have an array I would like to set as a cookie. The array has spaces in its elements. It breaks when I try to return it.
(unserialized) Array example is:
Array
(
[0] => U06 Bucks
[1] => U07 Stags
[2] => U09 Highlanders
)
To bake my cookie I have:
<?php
$page = $_REQUEST['page'];
if (isset ($_REQUEST['teams'])){
setcookie("team", serialize($_REQUEST['teams']),time()+31536000);
}
else {
// set the expiration date to past
setcookie("team", "", time()-31536000);
}
header('Location:'.$page);
?>
To unbake it, I have:
unserialize($_COOKIE["team"]);
Returns
Array ( [0] => U06 [1] => U07 [2] => U09 )
var_dump($_COOKIE)
gives me:
["team"]=> string(64) "YTozOntpOjA7czozOiJVMDYiO2k6MTtzOjM6IlUwNyI7aToyO3M6MzoiVTA5Ijt9" } array(3) { [0]=> string(3) "U06" [1]=> string(3) "U07" [2]=> string(3) "U09" }
According to my browser, the cookie looks like this:
- Name: Team
- Value: YTozOntpOjA7czozOiJVMDYiO2k6MTtzOjM6IlUwNyI7aToyO3M6MzoiVTA5Ijt9
Works fine without spaces in the array and I have tried json_encode via Storing and retrieving an array in a PHP cookie.
Any tips?