0

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?

Community
  • 1
  • 1
ash
  • 33
  • 4
  • You can try with base64 encoding and decoding. The problem could be some forbidden chars when making a cookie. – va5ja Apr 04 '14 at 10:02
  • 1
    What does "does not work" mean? `setcookie()` returns false? The HTTP header is not sent? Data gets corrupted? Browser sets on fire? – Álvaro González Apr 04 '14 at 10:07
  • I simply cannot reproduce. I'm pretty sure the issue is somewhere else (maybe in the way to test whether it "works"). – Álvaro González Apr 04 '14 at 10:14
  • Please: 1) Use your browser's developer tools to inspect the cookie storage and HTTP headers 2) `var_dump()` the raw `$_COOKIE` array and see the result in the "View Source" window. – Álvaro González Apr 04 '14 at 10:38
  • Var_dump gives me... array(3) { [0]=> string(3) "U14" [1]=> string(3) "U15" [2]=> string(3) "U18" } still breaks on the space with and without DanFromGermany's suggestions below. (I have changed the initial array values) – ash Apr 04 '14 at 10:41
  • @Álvaro G. Vicario I tend to agree with both of you that its not in the code listed. I know its another question... is there a better way to store an array in a cookie? – ash Apr 04 '14 at 10:48
  • It's very strange that `var_dump($_COOKIE)` does not contain any reference to the cookie name (`team`). How about point #1? – Álvaro González Apr 04 '14 at 10:51
  • @Álvaro G. Vicario sorry, I did edit but it did not go through... I get a cookie - Name: Team and Value: YTozOntpOjA7czozOiJVMDYiO2k6MTtzOjM6IlUwNyI7aToyO3M6MzoiVTA5Ijt9 – ash Apr 04 '14 at 10:55
  • @Álvaro G. Vicario var_dump($COOKIE) - thought you wer refering to the individual cookie. Gives me ["team"]=> string(64) "YTozOntpOjA7czozOiJVMDYiO2k6MTtzOjM6IlUwNyI7aToyO3M6MzoiVTA5Ijt9" } array(3) { [0]=> string(3) "U06" [1]=> string(3) "U07" [2]=> string(3) "U09" } – ash Apr 04 '14 at 11:03
  • `YTozOntpOjA7czozOiJVMDYiO2k6MTtzOjM6IlUwNyI7aToyO3M6MzoiVTA5Ijt9` is the Base64 encoding for `a:3:{i:0;s:3:"U06";i:1;s:3:"U07";i:2;s:3:"U09";}`. Are you positively sure that the code you've shared is the actual code you run? – Álvaro González Apr 04 '14 at 11:05
  • `$_COOKIE` is an array, thus `var_dump()`'s output must be enclosed with `array(...){...}`. Are you editing the data before posting it here? – Álvaro González Apr 04 '14 at 11:08

2 Answers2

0

Cookie data must be urlencode()d or rawurlencode()d. PHP does this by itself (setrawcookie() would not). So the source of the issue is still unknown. Try to base64_encode() the cookie and test again:

The cookie data is sent as a header, which value is a sequence of characters excluding semi-colon, comma and white space. (Refere to http://curl.haxx.se/rfc/cookie_spec.html for the exact definition and specs).

setcookie("team", base64_encode(serialize($_REQUEST['teams'])) ,time()+31536000);

$team = isset($_COOKIE['team'])
      ? unserialize(base64_decode($_COOKIE['team']))
      : array();
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • Thanks DanFromGermany, I had mucked around with the base64 encode decode, but no luck... I think think the order of `base64_decode(unserialize($_COOKIE['team']))` should be the other way round too. If no white spaces are allowed, then could I replace both ways with some non spaces? – ash Apr 04 '14 at 10:28
  • @ÁlvaroG.Vicario The whole question got obsolete as you already said the problem is somewhere else and it can't be reproduced. – Daniel W. Apr 04 '14 at 10:28
  • @ash On using `setcookie`, PHP already replaces the white spaces, your issue, as already mentioned, is not in the code shown above. – Daniel W. Apr 04 '14 at 10:30
0

The cookies must be urlencoded - use urlencode().

UPDATE

Well, setcookie() urlencodes the data internally, so the problem is somewhere else!

blue
  • 1,939
  • 1
  • 11
  • 8