I'm working on a web app that requires a certain cookie to be set, and it seems like setting the cookie is taking place AFTER the web script checks for it. The first time the web page is loaded (and this has been tested in three different web browsers) the script cannot find the cookie, even though if you look (for instance, in Web Inspector in Chrome) the cookie is clearly set.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.