in reply to Cookies Not Getting Set First Time
If you are reading it in the same script that you set it in (and on that run - it will be available if the script is called again), it will not contain the cookie you set.
Nothing wrong with your initial method of retrieval, as far as i can see.
Look at your cookie header code. any reason why you are setting $cookie twice? See docs - looks like you mis-copied their example that set two cookies.
But in answer to *why* it's not there, you need to look at the cgi.pm module itself. When retrieving cookies, cgi calls CGI::Cookie->fetch, when setting, it returns a cookie object created by CGI::Cookie(@param).
Don't make the mistake of thinking of the $cgi->cookie call as being an object - it's a method, context depending on how it's called.
If you wanted it to work as you expect, you'd need to fix things a little. I suggest you create a %cookie hash and add name value pairs to it and then use an array to send to CGI. EG,
#!/usr/bin/perl use strict; use CGI; use CGI::Carp 'fatalsToBrowser'; my $cgi = new CGI; # grab existing %cookie my %cookie; for ($cgi->cookie()) { $cookie{$_} = $cgi->cookie($_); } # set a couple of cookies; $cookie{'botLoginName'} = "XXXXX:YYYYY:ZZZZZ"; $cookie{'Other_cookie'} = 'whatever'; # create cookies for CGI my @cookies; for (keys %cookie) { push @cookies, $cgi->cookie( -name => $_, -value => $cookie{$_} ); } # print amended header print $cgi->header(-cookie=>[@cookies]),$cgi->start_html(-title=>'Logi +n'); # use our hash to check "cookie" if(defined(my $cookieHolder = $cookie{'botLoginName'})){ print $cgi->h3("Retrieved cookie named botLoginName: $cookieHo +lder"); } else{ print $cgi->h3("No cookie retrieved."); } # display all stored print $cgi->h4('Stored Cookies'); for (keys %cookie) { print "$_ = $cookie{$_}".$cgi->br; }
But, having said that, it's probably better to write your code so that on first run (or run where username is sent) that you just check the param() rather than cookie()
HTH
cLive ;-)
|
|---|