If you reload the page, then the cookie is found, of course, but Im setting the cookie before I ever send anything to the browser, so why isn't it found immediately? At first I tried putting the check and setting of the cookie in a perl module that's called by all of the scripts involved, but that didn't work. Thinking that this had something to do with mod_perl, and the module only being compiled once, I made it a sub in the module and call the sub in the script itself.
That produces the same result. Running it on a non-mod-perl apache instance also produces the same result. I"ve got a demo code that will do this, a .pm file and .pl file that's actually run as the cgi script.
A test page is here
It will display the returned result from running the cookie-setting sub, then display the result of the check.
Here's the code :
use strict; #use warnings; use CGI qw(param :standard); require Exporter; our @ISA = qw(Exporter); our %EXPORT_TAGS = ( 'all' => [ qw($MyCookie $PrintHeader CheckCookie) + ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = qw($MyCookie $PrintHeader CheckCookie); our $VERSION = '1.0'; our $PrintHeader = "Content-type: text/html\n\n"; our $MyCookie = "ARandomCookie"; sub CheckCookie { my $affil = cookie($MyCookie); if ($affil eq ''){ $affil = "It Should Be Set!!"; my $packedURLcookie = cookie( -NAME => $MyCookie, -VALUE => "$affil", -path => "/"); print "Set-Cookie: $packedURLcookie\n"; return 1; } } 1;
And the actual script:
#!/usr/bin/perl use strict; use CGI qw(param :standard); use CookTest; my $subres = &CheckCookie; my $alleged = cookie($MyCookie); print $PrintHeader; print <<EOF; <html> <title>Cookie tester</title> <body> My sub cal results are $subres <p> My alleged cookie is $alleged <p> </body> </html> EOF exit;
In reply to Perl + web cookies, not sure why this is failing by desertrat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |