Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delayed Cookie Updates

Status
Not open for further replies.

Tyger

Programmer
Sep 19, 2001
21
GB
If I write new data to a cookie and then use $ENV{'HTTP_COOKIE'} to display the cookie's content I always get the PREVIOUS cookie value, not the value I have just written.

Even if the "write cookie" and the "read and display cookie" routines are stored in separate scripts it still doesn't display the new value. I have tried using the SLEEP command for as much as 10 seconds between storing and retrieving cookie data and it doesn't solve the problem.

The only way around it is to store the data to be written in a variable (this is usually required anyway), then write the variable data to the cookie and display the VARIABLE instead of $ENV{'HTTP_COOKIE'}. This ensures the current value is displayed, but this method is not always convenient.
 
Hi,

Forgive me if I've misunderstood, but it seems you're not actually sending the cookie to the client, so of course the value of $ENV{HTTP_COOKIE} won't change. You need to create header, including the "Set-Cookie: mycookie=mycookieval" line, and send it to the client. The client will now have a cookie "mycookie=mycookieval".
The next time the client requests your script, the cookie will be sent along with the request and you can retrieve the value from $ENV{HTTP_COOKIE}.


1. Client requests "myscript.pl", including a previously set cookie "mycookie=mycookieval1";
2. myscript.pl checks for a cookie in $ENV{'HTTP_COOKIE'},
and gets "mycookie=mycookieval1". The script does some stuff resulting in the new value you want to set in the cookie. It then prepares the header to send to the client, including the "Set-Cookie: mycookie=mynewcookieval" line, then sends the header, followed by the body of the document.
3.Client now has a cookie "mycookie=mynewcookieval".
4. Client again requests myscript.pl, with the new cookie "mycookie=mynewcookieval".
5. myscript.pl retrieves the new cookie value from $ENV{'HTTP_COOKIE'} and so on.

Let me know if I'm on the wrong track

Cheers

fieldee
 
I have used Set-cookie etc. The problem is that if I try to read the new value from the cookie, within the same script which has just set it (Or within a subroutine called from the same script) the cookie contains the previous value, not the new value I have just set.

Your reply implies that the new value will not be in the cookie until the script has been fully executed, and thus the new value cannot be retrieved until the script next runs. If this is the case, then all makes sense.

Thanks for the help.
 
This is a simple solution but maybe not as desirible as you want.

You might look at making a redirect page that they are sent to to verify the cookie being set and then return to the page with your script to pick up the new cookie.

You cannot set and read a cookie at the same time. This is unnecessary. Since when you set the cookie your script has the information in the new cookie already. The script has to be re-executed to pick the new cookie up. Cookies are used to maintain state infromation between script calls.

Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top