473,749 Members | 2,486 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 2348
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
11319
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
2466
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 (quiz1_results.htm), in turn, shows the score. The JS in the quizconfig.js appears to set a...
2
1390
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
2974
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
1604
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
3008
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 stores the cookie in memory. Is there a way to have PHP just read a cookie from the desktop machine?...
12
2339
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(); $atoday=getdate($today); //Get the date into an array $atoday->mon++; //set the month to next...
2
1346
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 values. However, I get this error:
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9388
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6800
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
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
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.