Cookies - either not saving or not reading correctly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
TimM
Forum Newbie
Posts: 7
Joined: Fri Dec 19, 2003 8:19 pm
Location: Canberra, Australia

Cookies - either not saving or not reading correctly

Post by TimM »

Ok, having trouble now with cookies. Here's what I've got:

cookietest1.php

Code: Select all

<?php
setcookie("testcookie", "testvalue");
?>
<a href="cookietest2.php">click</a>
cookietest2.php

Code: Select all

<?php
print ($_COOKIE["testcookie"]);
?>
I was having trouble before this, and narrowed it down to the cookies, so I created these two files to try to figure out why it wasn't working. But, I'm stumped! I checked IE's privacy settings, even changed them to the lowest possible, and still no luck.

Basically what I've done is created cookietest1.php to set the cookie, then it has a link to cookietest2.php which reads the cookie. The output from cookietest2.php is just blank - as if the cookie had never been set. No errors or anything.

Is there something here I'm doing wrong, or is just my unlucky day?

Thanks

- Tim
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Does giving the cookie a time limit make any difference?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

are you setting the cookie is a subfolder of the site, and trying to access it from a parent folder? (you may want to set the path argument)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

I think it would be an idea to see what is working - the setting or the reading.

Clear you IE cache and temp files. Run cookietest1.php and then check in the temp internet files to see wether the cookie has been created.

This will answer the above question, then we can concentrate on the part that actually isn't working.

Mark
User avatar
TimM
Forum Newbie
Posts: 7
Joined: Fri Dec 19, 2003 8:19 pm
Location: Canberra, Australia

Post by TimM »

Well, what would you know, now it works..just like that! :roll:

Thanks Mark, that was a great idea. I was just doing that then when I realised that its now working. And I didn't change a thing.

Thanks anyway, everyone, for being willing to help out!

Cheers
Tim
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

Grim... wrote:Does giving the cookie a time limit make any difference?
If a time value isnt set, the cookie will delete as soon as the browser is closed.

hence why u should always set a time()
User avatar
TimM
Forum Newbie
Posts: 7
Joined: Fri Dec 19, 2003 8:19 pm
Location: Canberra, Australia

Post by TimM »

If a time value isnt set, the cookie will delete as soon as the browser is closed.

hence why u should always set a time()
Unless you're using it to track a session - which is what's happening in this case :)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

I know, it just seemed he was asking in general.
Post Reply