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

This script works fine:
#!/usr/local/bin/perl use CGI; $q = new CGI; $cookie_out = $q->cookie(-name=>"wolves", -value=>"cookieinfo",-expires=>'+24h'); print $q->header(-cookie=>$cookie_out); $cookie_in = $q->cookie("wolves"); print $cookie_in;
It prints the contents of $cookie_in when i hit reload on the browser. But if i cut this part of the script out:
use CGI; $q = new CGI; $cookie_in = $q->cookie("wolves"); print $cookie_in;
and create a new script, the new script does not read the cookie back in. It only works if i have the read and write cookie lines in the same script, which kind of ruins the purpose of having a cookie. -Lisa

Replies are listed 'Best First'.
Re: Crumby cookie
by Hero Zzyzzx (Curate) on Nov 21, 2002 at 05:04 UTC

    Try explicitly setting a path in the cookie you're creating, one broad enough to encompass BOTH scripts you're testing. So:

    $cookie_out = $q->cookie(-name=>"wolves", -path=>'/', #or -path=>'/cgi +-bin/' for your cgi-bin directory -value=>"cookieinfo",-expires=>'+24h');
    Will make it valid for your entire site. When an explicit path is not set, browsers may interpret where a cookie is valid for differently. Where does that get you?

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

      The Path thing fixed it.. woof meow. -Lisa
Re: Crumby cookie
by blaze (Friar) on Nov 21, 2002 at 01:11 UTC
    I used your code to set ( i just added use strict and warnings) and mine to read and it worked fine
    #!/usr/local/bin/perl -w # # this is a file i named set.cgi # # use strict; use CGI; my $q = new CGI; my $cookie_out = $q->cookie(-name=>"wolves", -value=>"cookieinfo", -expires=>'+24h'); print $q->header(-cookie=>$cookie_out); my $cookie_in = $q->cookie("wolves"); print $cookie_in;


    #!/usr/local/bin/perl -w # # this is my code, i named it read.cgi # # use CGI; use strict; print "Content-type:text/html\n\n"; my $q = new CGI; my $cookie_in = $q->cookie('wolves'); print $cookie_in;
    both pages displayed the word "cookieinfo", do you get something different?
    -Robert
      I copied your code exactly into 2 scripts and ran them. The first script works fine.. but the second scripts prints nothing on the screen. As i stated before, if i put both the output and input cookie lines on the same script, hitting reload does print the the value of cookie_in when i hit reload. It's when i split it into 2 scripts that the second script does nothing. Bummer. -Lisa
        PS.. i know the cookie is landing on my hard drive, cause i see it arriving in my cookie folder. The second script just isn't reading or seeing it for some reason. When i modify the script, i delete the cookie and watch it appear again when i run the script.. i must have deleted it a 100 times. -Lisa
Re: Crumby cookie
by blaze (Friar) on Nov 21, 2002 at 00:58 UTC
    what exactly are you trying to do, if you cut out use CGI; you wont be able to read or write a cookie using $q->cookie("wolves"); so i guess i dont exactly understand what you are trying to do...but if you already set the cookie, which it seems you have, a little script like
    #!/usr/bin/perl -w use CGI; use strict; print "Content-type:text/html\n\n"; my $q = new CGI; my $cookie_in = $q->cookie('wolves'); print $cookie_in;
    should read the cookie and print it to the page
    Warning: Code Untested
    hth,
    Robert
Re: Crumby cookie
by wolverina (Beadle) on Nov 20, 2002 at 22:51 UTC
    PS.. both cookies are on the same server/same domain. -Lisa