471,624 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

Cookie array does not work ?

Why does the following work:

// (1) Set new cookies
setcookie ("font_type", $type_sel, time()+3600);
setcookie ("font_size", $size_sel, time()+3600);
// (1) Get most recent cookie
$font_type = $_COOKIE["font_type"];
$font_size = $_COOKIE["font_size"];
if (isset($font_type) && isset($font_size))
echo "Found Cookies<br>\n";

But this does not work:

// (2) Set new cookies
setcookie ("font[type]", $type_sel, time()+3600);
setcookie ("font[size]", $size_sel, time()+3600);
// (2) Get most recent cookie
$font[type] = $_COOKIE["font[type]"];
$font[size] = $_COOKIE["font[size]"];
if (isset($font[type]) && isset($font[size]))
echo "Found Cookies<br>\n";

I took (2) from the book Beginning PHP4 (Wrox), page 282-286. I did
(1) myself in an effort to get the book code working.

When (2) is executed the if statement in never true.

Note: $type_sel and $size_sel, in (1) and (2), both have valid values
at the time the cookie is set.

Note 2: I appreciate that I shouldn't be calling my page variables by
the same name as my cookie variables but that is not the problem - it
does not explain why (1) works but (2) does not work.

What am I doing wrong?
Oct 10 '05 #1
2 2243
You can't do anything with cookies maked in this file open!

Sorry for bad grammar, I'm from Polish :)

Oct 10 '05 #2

Harry Haller wrote:
But this does not work:

// (2) Set new cookies
setcookie ("font[type]", $type_sel, time()+3600);
setcookie ("font[size]", $size_sel, time()+3600);
// (2) Get most recent cookie
$font[type] = $_COOKIE["font[type]"];
$font[size] = $_COOKIE["font[size]"];
if (isset($font[type]) && isset($font[size]))
echo "Found Cookies<br>\n";


a) the newly set values won't be avail in the $_COOKIE array until the
next time you load the page.
b) by using the []s php assumes you want to use an array-like
structure...
$_COOKIE now looks like array(
'font' => array(
'type' => x,
'size' => y,
),
any other values....
)

print_r($_COOKIE); // print_r is your friend

so you'd access 'size' via $_COOKIE['font']['size']

note: you're actually setting/saving 2 discret cookies this way..
php just creates the single font array.
I prefer to manually serialize/unserialize.

Oct 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Phil Powell | last post: by
1 post views Thread by Display Name | last post: by
2 posts views Thread by Raghu Raman | last post: by
3 posts views Thread by Ron Croonenberg | last post: by
29 posts views Thread by Jerim79 | last post: by
1 post views Thread by XIAOLAOHU | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.