Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Cookie value not getting set

by amulcahy (Acolyte)
on Jan 28, 2002 at 21:02 UTC ( [id://142115]=perlquestion: print w/replies, xml ) Need Help??

amulcahy has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

I have been using cookies quite succesfully until I got to this point. The script has a button for 'Save Settings', associated with a textfield called 'spcSaveName'. When this button is clicked the script is re-opened and new info is displayed. The cookie part of the script has:
$spcName = $query->cookie(-name=>'spcName', -value=>$query->param('spc +SaveName')); print $query->header(-cookie=>[$spcName]);
at the top. Straight after this I test the values with:
print $query->param('spcSaveName'); print $spcName; print $query->cookie(-name=>'spcName');
When I click Save the first time $query->cookie(-name=>'spcName') has no value. If I click 'Back' and 'Save Settings' again, then the cookie value is correct?
Any ideas why this only works after the second click? Also, if you are wondering if there is any other code there that might be affecting the cookie creation, there's not. I even commented out everything else, just in case.
Thank you.

Replies are listed 'Best First'.
Re: Cookies
by dvergin (Monsignor) on Jan 28, 2002 at 22:25 UTC
    If I follow what you are saying, the cookies are working as advertised. Here's my rephrased walk-through of what you seem to be describing.

    • In a CGI script activated by a "Save" button in a form you set a cookie by printing it out in the header.
    • In that same script you then test for the cookie.
    • But at that point the cookie is not there.
    • Later (another page load) the cookie is there.

    The current CGI script gets its cookie values from the remote client browser at the script start-up. The cookie value you set is still in the stream of output to the user's browser when you print it and in all subsequent parts of the current script.

    The cookie is not available (to be checked or used) until you get a response back from the client browser which has received the cookie and then sends it back to a script at your end for use (either the same script or a different one).

    Does this help?
     

Re: Cookies
by earthboundmisfit (Chaplain) on Jan 28, 2002 at 22:27 UTC
    Update: sorry for the dup

    It sounds like a timing thing to me. At the time the script is compiled, there is no cookie data available. After it runs the first time, the client browser now holds valid cookie data which is passed to the script on the second try. You might try forking a new process after setting the cookie and placing your print statement within the child. Silly me. That won't work since the fork just copies what you had at pre-compile.

    So you're left with something like:

    #! Perl -w use CGI; use strict; my $query = new CGI; my $spcName = $query->cookie(-name=>'spcName', -value=>$query->url_pa +ram('test')); print $query->header(-cookie=>[$spcName]); print "cookie value: ", $query->cookie(-name=>'spcName'), "<BR>"; my $savedVal = $query->cookie(-name=>'spcName') | $query->url_param(' +test'); print $savedVal;

    -------
    Glad is the heart that beats not for itself alone

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://142115]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-16 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found