473,544 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ty pe) && isset($font_siz e))
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 2341
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($_COOKI E); // 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
11287
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 ($hasLoggedIn && ($row = mysql_fetch_row($query))) { setcookie('nordicnet_registration', $row, 0, '/'); @mysql_free_result($query);
1
2443
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; client wants the results from the quiz to go into the email. The file "quizconfig.js" parses the form-submitted test results & the test results page...
2
1384
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 ="myarray=" + myarray;
3
2963
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 want to check all cookies that start with "first" in there name. I tried to work wih string and substrings, but that didn't seem
1
1590
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
2985
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 switch/case to take action based on that value in the cookie. I want to set the cookie manually, so I don't need setcookie(). I see that in PHP it...
12
2321
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 cookie on the html page, and none of those have worked. <?php session_start(); $rating1=$rating; session_destroy(); $today=time(); ...
2
1334
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 for each x in Array Response.Cookies("cookie")(count) = x count = count + 1 next This should give a list of cookies that contain the array...
0
7452
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7643
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7798
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7405
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5956
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5316
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3436
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1862
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.