Page 1 of 1
Can $_COOKIE ever have subarrays?
Posted: Sat Oct 01, 2005 11:57 pm
by Ambush Commander
It's common knowledge that:
Code: Select all
$_COOKIE
cookiename => value
cookiename => value
...
Is it ever possible that...
Code: Select all
$_COOKIE
cookiename => array(value1, value2)
...
I'm asking this because I've made an iterative de-magic-quote function, but I don't want to call it unnecessarily because in non-array based inputs, it performs slightly worse.
Posted: Sun Oct 02, 2005 12:14 am
by Weirdan
Have you tried that? It's as easy as:
Code: Select all
if($_COOKIE) {
var_dump($_COOKIE);
}
$_COOKIE['test'] = array('test', 'another one');
pointing your browser to the above page and hitting refresh
Posted: Sun Oct 02, 2005 3:19 am
by wwwapu
Posted: Sun Oct 02, 2005 4:53 am
by Weirdan
PHP manual wrote:
You may also set array cookies by using array notation in the cookie name. This has the effect of setting as many cookies as you have array elements, but when the cookie is received by your script, the values are all placed in an array with the cookie's name:
Example 3. setcookie() and arrays
Code: Select all
<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
echo "$name : $value <br />\n";
}
}
?>
which prints
Code: Select all
three : cookiethree
two : cookietwo
one : cookieone
Posted: Sun Oct 02, 2005 11:04 am
by Ambush Commander
Meh. Should've checked the manual. Well, since I'm not going to use any cookies that do that, I'll effectively have no cookies like that... but it's a good thing to know.