473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2334
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Phil Powell | last post by:
Fourth attempt.. it fails now in login, I check by printing $_COOKIE and there is no value there! Guys, what on earth do I do about this???? Here is the code that sets the cookie: if...
1
by: Display Name | last post by:
Used one of these canned scripts to set up a JS quiz but not before having used another canned PHP script for "Tell your friend about this Web page!" sort of thing. Now i've gotta integrate them;...
2
by: Raghu Raman | last post by:
Hi, In javascript file i store 3 values under a single cookie array ,like var myarray = new Array(null); myarray = "field_1"; myarray = "field_2"; myarray = "field_3"; document.cookie...
3
by: Ron Croonenberg | last post by:
Hi all, cookies are stored in an array called $HTTP_COOKIE_VARS right ? Is it possible to find a cookie if you know part of it's name ? For example the cookiename is firstname and I...
1
by: flutetones | last post by:
http://67.189.52.24/~metafusionserver/public_html/training.php Here is a link to my server. I have an issue that doen't make sense. What's hapening is this . . . What's going right . . .
29
by: Jerim79 | last post by:
I did try to find the answer to this before posting, so this isn't a knee jerk reaction. What I am trying to accomplish is to have a script that opens a cookie, reads a value, and then use a...
12
tpgames
by: tpgames | last post by:
I've tried JS cookie on the html page that contains the iFrame code linking to the php jigsaw puzzle game. That did NOT work. I've tried JS cookie on the php page; php cookie on the php page, php ...
2
by: zensunni | last post by:
I'd like to assign an array to a cookie. But, I can't incrementally assign cookie names, thus, I can't assign different values. Here is the code that should work, but doesn't let me. count = 0...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.