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

Hello again.
Today I am being harassed by the evil CGI::Cookie which, I understand isn't supposed to be evil. Anyway, here's a segment of my script:
use strict; use CGI qw(:all); use CGI::Cookie; my ($domain, $name, $value, $expires) = ('65.1.136.248', 'SessionID', +$sessionID, '+1Y'); my $cookieheader = new CGI::Cookie(-name => $name, -value => $value, -expires => $expires, -domain => $domain) || die; print header(-cookie=>$cookieheader); my $badcookie = fetch CGI::Cookie('SessionID'); print $badcookie;
My error.log returns:
e:\apache\htdocs\cgi-bin\VCG-LO~1.CGI: Use of uninitialized value in p +rint at e:\apache\htdocs\cgi-bin\VCG-LO~1.CGI line 41.
As you probably see, I'm using Apache on Win32 (Win2k to be exact).
Line 41 is the: print $badcookie;

I checked my local cookies and it does exist. The $ENV{HTTP_COOKIE} seems to non-existant.
Thanks in advance.
--
paul

Replies are listed 'Best First'.
Re: Cookie Woes
by slayven (Pilgrim) on Oct 20, 2001 at 05:24 UTC
    maybe this way around:
    # untested my %cookies = fetch CGI::Cookies; if (exists $cookie{SessionID}) { # gotya my $cookie = $cookie{SessionID}; # i once spent two hours debugging before i # realized that $cookie isn't a scalar :-/ use Data::Dumper; print Dumper($cookie); }
      Mine dies at the cookie retrevial, if I say: my %cookies = fetch CGI::Cookies || die; It will die. If I go open the cookie file, everything is there.
      --
      paul
Re: Cookie Woes
by cLive ;-) (Prior) on Oct 20, 2001 at 13:05 UTC
    This will probably answer your question (similar, but just using CGI).

    cLive ;-)

    Edit kudra, 2001-10-22 Changed link to use [] syntax

      Okay, that worked .. ONCE. What I mean is, I ran the 'login' script, which created the cookie, then i linked to a 'test' script, which asks for the cookie. The test script displayed the contents of the cookie, but after i hit reload, nothing would come back. I tried this in IE, Mozilla 0.8.1, Netscape 4.x, and Netscape 6.x. Each with the same result.
      --
      paul
        OK, one more question. You set the IP address for the cookie. When the script is called again, is it on the IP address, or on a domain name that is on that IP address?

        If the latter, then change the IP to the domain name when setting the cookie and see if that works.

        cLive ;-)

Re: Cookie Woes
by George_Sherston (Vicar) on Oct 20, 2001 at 16:50 UTC
    I don't quite understand these questions but I have two comments that might be relevant. Please don't treat these as authoritative, though.

    (1) did you define $sessionID somewhere else? If not, the script as will fail under strict, as well as your cookie being deficient.

    (2) for some reason I don't understand, $badcookie seems to be a hash reference rather than a scalar.

    Taking these together I get your script to run amended thus:
    use strict; use CGI qw(:all); use CGI::Cookie; # insert definition of $sessionID: my $sessionID = "hello world"; my ($domain, $name, $value, $expires) = ('65.1.136.248', 'SessionID', +$sessionID, '+1Y'); my $cookieheader = new CGI::Cookie(-name => $name, -value => $value, -expires => $expires) || die; # left out domain so it wd work on my web space print header(-cookie=>$cookieheader); my $badcookie = fetch CGI::Cookie('SessionID'); # print $badcookie as hashref, not scalar: print $badcookie->{'SessionID'};


    § George Sherston
      Okay, tried that.
      $sessionID is setup somewhere else.
      With your code, the cookie is created, but nothing is returned. Per cLive ;-), I put the cookie-recall sub into a different script. This will return the cookie once, but not again, after a reload. I have the same results in IE, Netscape 4.x, Netscape 6.x, and Mozilla 0.8.1.

      --
      paul
Re: Cookie Woes
by vbrtrmn (Pilgrim) on Oct 22, 2001 at 09:58 UTC
    Another quickie.. in reference to $ENV{'HTTP_COOKIE'}. It never gets set, is my script supposed to set it?
    --
    paul