Need PHP to see cookie on first reload, not second.

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
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Need PHP to see cookie on first reload, not second.

Post by JAB Creations »

PHP seems to have a problem with not seeing a cookie on a reload and will not see it until the page reloads twice! This creates a timing issue in a sense, what is possibly causing this and how do I fix it?

John
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

A coding, or more likely a user error on your part will be causing this.

Please post your code, else we will be unable to help.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Hiya Jenk, here you go...

Cookie
<?php if ($HTTP_COOKIE_VARS[namehere]=="1") {...} ?>
John
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

How and where are you setting the cookie?

also, $HTTP_COOKIE_VARS array is now deprecated, use $_COOKIE instead (it's also quicker to type for the lazy types like me).
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

PHP reads cookie on first reload...

Post by JAB Creations »

Thanks Jenk, that was exactly the problem!

Here is an example for others to test it out with...
<?php
echo $_COOKIE[example];
?>
<br />
<a href="#" onmousedown="document.cookie='example=1;path=/;time()+314496000;'" onmouseup="window.location.href=window.location.href">1</a> - <a href="#" onmousedown="document.cookie='example=2;path=/;time()+314496000;'" onmouseup="window.location.href=window.location.href">2</a>
John
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

also, you should be using the $_COOKIE array like so:

Code: Select all

echo $_COOKIE['example'];
note the quotes, if the key is a string it must be treated as a string. :)
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Key? My cookies are a name, value, domain, and expiration date only. Am I missing something?

Single value no quotes, key requires quotes?

John
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

you put

Code: Select all

echo $_COOKIE[example];
instead of

Code: Select all

echo $_COOKIE['example'];

when a value appears that is not a string (specified by surrounding it with single quotes) php will look for a constant with that name, if it can't find it it assumes a string [and throws an E_NOTICE error if I'm not mistaken], you probably wouldn't notice any ramifications in the way you're using it but you could sometimes have a constant with the same name as an array key in a larger application
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Thanks, where can I find logged errors? I'm running a local install of Apache and PHP.

John
Post Reply