Forum Moderators: coopster

Message Too Old, No Replies

cookie timing issue

set cookie & immediatly read it and it is blank

         

danp55

11:44 pm on Feb 17, 2006 (gmt 0)

10+ Year Member



Cookies should be easy. That being said, I have a real confounding problem.
I set a cookie, and immediatly print the cookie variable.
It comes back blank.
-------------------------------------------------
Here is my script, t2.php:
<?php
$password1 = 'passwd123';
Setcookie("lct1" ,$password1);
$gt1= $lct1;
print '------ in cookie test ------<br>';
print 'gt1=['. $gt1 .']<br>';
print 'password =['.$password1 .']<br>';
print '------ end cookie test ------<br>';
include("t4.php");
?>
-------------------------------------------------
Notes:
Tried these on several computers with cookies enabled.
Tried these with I.E., Netscape & Firefox.
All with the same results.

-------------------------------------------------
TEST PROCESS:
1. cleared ALL cookies.
2. ran t2.php.
3. got these results:

------ in cookie test ------
gt1=[]
password =[passwd123]
lct1 =[]
------ end cookie test ------

4. looked at my cookies. I had 1, 'lct1'
5. ran t2.php AGAIN.
6. Got these results

------ in cookie test ------
gt1=[passwd123]
password =[passwd123]
------ end cookie test ------

It has the appearance as though after the 'setcookie' command, there is not enough processing time to have
the value set before the value is retrieved.

Please try this simple test and let me know?

Or if you see what I am not seeing please let me know.

whoisgregg

12:36 am on Feb 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



setcookie() adds a header that is sent to the user along with their request.

On subsequent requests, users will send that cookie back in their request. (If they so choose.)

$_COOKIE is an array of all cookies that the user has sent during the current request.

So, the request your setcookie() is available during is the next request -- not the current request.

danp55

12:47 am on Feb 18, 2006 (gmt 0)

10+ Year Member



Thanks.
That actually made sense.
Time to rework the code.
Danp55

DrDoc

4:14 am on Feb 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It may be important to note that for session cookies it is actually possible to read the values right after they have been set (and thus also before sent to user), simply because they are stored on the server. The session cookie only holds a session ID, which is used to retreive the appropriate session data.