2

In the php manual under section "cookies" it states that you can add multiple values to a single cookie by simply adding a '[]' to the cookie name.

At first my understanding of this was to do the following:

<?php
setcookie("[user1]", "bill");
setcookie("[user2]", "tom");

print_r($_COOKIE);
?>

and the out put was: 'Array ( )' Obviously after seeing an empty array I knew something was not right but, I had followed what the manual had stated. So I went in developer mode in the browser to see all header information of the browser and server.

Browser and Server both had the following: [user1]=bill [user2]=tom

yet the $_COOKIE associative array was empty i.e. 'Array()'

So I researched and found under the PHP manual the way several values are stored in a single cookie under section, 'Variables From External Sources.'

Here it gives a detailed example of how this is correctly done. Instead of the above, it is done as follows:

<?php
setcookie("Cookie[user1]", "bill");
setcookie("Cookie[user2]", "tom");

print_r($_COOKIE);
?>

Output for the above script is: 'Array ( [Cookie] => Array ( [user1] => bill [user2] => tom ) ) '

My question is why in the first example do the cookies register and yet don't print out but in the second (correct) example they do print out in the $_COOKIE variable?

Brombomb
  • 6,988
  • 4
  • 38
  • 57
Robert
  • 10,126
  • 19
  • 78
  • 130
  • 1
    correct example is – Robert Nov 12 '12 at 06:03
  • i have edited your question according to your comment if you think its effecting your question than rollback – NullPoiиteя Nov 12 '12 at 06:11
  • @NullPointer I think you edited the wrong section. I can't rollback your change to fix. Should have fixed the second code block. – Brombomb Nov 12 '12 at 06:18
  • @Brombomb ..all right ... i have rolled it back – NullPoiиteя Nov 12 '12 at 06:21
  • [user1] is not the way to declare an array, so it doesn't set the cookie corectly. On the second example, you are creating an array with the name "Cookie" inside the $_COOKIE variable. So, this should output the contents of that array: $_COOKIE["Cookie"] – alex.ac Nov 12 '12 at 14:31

1 Answers1

4

You are doing it slightly incorrectly

 setcookie("Cookie[user1]", "bill");
 setcookie("Cookie[user2]", "tom");

This will store the values 'bill; and 'tom' as an array, inside a cookie called 'Cookie', which is accessed via the $_COOKIE supoer global. you need to access it like this:

print_r($_COOKIE['Cookie']); // Cookie is the name you used above

Another example:

setcookie("bob[user1]", "bill"); // cookie name is bob
print_r($_COOKIE['bob']); // Notice the cookie name is the key here

If you want to store an array inside a single cookie you can also serialize the contents. This will convert the array to a string to be stored inside the cookie, you can then convert it back when you need the data.

$myArray = array(1,2,3,4);
setCookie('my_cookie', serialize($myArray)); // Store the array as a string
$myArray = unserialize($_COOKIE['my_cookie]); // get our array back from the cookie
Andrew
  • 12,617
  • 1
  • 34
  • 48