Page 1 of 1

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

Posted: Wed Jan 18, 2006 5:52 pm
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

Posted: Wed Jan 18, 2006 5:53 pm
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.

Posted: Wed Jan 18, 2006 5:58 pm
by JAB Creations
Hiya Jenk, here you go...

Cookie
<?php if ($HTTP_COOKIE_VARS[namehere]=="1") {...} ?>
John

Posted: Wed Jan 18, 2006 6:04 pm
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).

PHP reads cookie on first reload...

Posted: Wed Jan 18, 2006 6:22 pm
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

Posted: Wed Jan 18, 2006 6:55 pm
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. :)

Posted: Wed Jan 18, 2006 11:46 pm
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

Posted: Thu Jan 19, 2006 12:33 am
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

Posted: Thu Jan 19, 2006 11:11 am
by JAB Creations
Thanks, where can I find logged errors? I'm running a local install of Apache and PHP.

John