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

I set a cookie like so:

my $cookie = $q->cookie( -name => 'sessionID', -value => \@sessionID, -expires => '+1h', -path => '/admin/', -domain => '.mydomain.com', -secure => 0 );

I proceed to print the cookie out with an HTML header. I can see that the cookie is successfully set by viewing cookies in my browser:

Name: sessionID Information: username&password Domain: .mydomain.com Path: /admin/ Server Secure: no

I then check for the cookie:

if ($q->cookie("sessionID")) { ($submitted_user, $submitted_pass) = @{ $q->cookie("sessionID") }; } else { error("Invalid user/pass. Please login again."); }

And it displays an error. So I print out the contents of $q->cookie('sessionID'); and it is empty, nothing there. Any ideas what the problem could be? Thanks.

Replies are listed 'Best First'.
Re: Empty Cookie
by bobn (Chaplain) on Jun 27, 2003 at 14:12 UTC
    Is your webserver's cgi path really ServerRoot/admin, i.e. you're using "http://www.mydomain.com/admin/script.pl" or "http://www.mydomain.com/admin/subdir/script.pl"as your URL?

    Also, it works for me, except that $q->cookie("sessionID") returns a list, not an array reference.

    ($submitted_user, $submitted_pass) = $q->cookie("sessionID");
    Update: here's my working code at http://www.bob-n.com/cgi-bin/test4.pl. Click once to get the cookie sent, reload within 10 seconds to see results of sending it back.

    #!/usr/bin/perl -wT use CGI; my $q = CGI->new(); use Data::Dumper; use strict; my ( $submitted_user, $submitted_pass, @rc, @sessionID); @sessionID = qw( PASS USR); if ( @rc = $q->cookie('sessionID') ) { print $q->header(), $q->start_html, $q->pre(['recv cookie', Dumper +(\@rc)]); if ($q->cookie("sessionID")) { ($submitted_user, $submitted_pass) = $q->cookie("sessionID"); print $q->pre([ '$submitted_user, $submitted_pass: ', "$submitted_user, $submitted_pass\n"]); } else { print ("Invalid user/pass. Please login again."); } } else { my $cookie = $q->cookie( -name => 'sessionID', -value => \@sessionID, -expires => '+10s', -path => '/cgi-bin/', -domain => '.bob-n.com', -secure => 0 ); print $q->header(-cookie=>$cookie), $q->start_html; print "<pre>sent cookie:\n", Dumper($cookie), '</pre>'; } print $q->end_html;


    --Bob Niederman, http://bob-n.com

      Thanks for the help. I can't see any problem with the code I've posted, and judging by the fact I can print the cookie but not read it I'd say it has to be either a problem with the version of CGI.pm or with the domain/path settings. I'll try a few things out and post the results if I figure it out.

Re: Empty Cookie
by gmpassos (Priest) on Jun 28, 2003 at 01:50 UTC
    Well, I don't know where is your problem. But what I have noted is that you are not showing your domain in your example. So, are you using "localhost" to test?

    If you are using localhost ($ENV{HTTP_HOST} = 'localhost'), you can't set explicity a cookie for this host for security, since your Brownser won't let the server set the cookie. So, when you set a cookie to localhost you can't send the domain part!

    Make some tests, sending only the name and value of the cookie, since is simpler to find the erro isolating it.

    Other thing you need to know, and probably already know, is that you need to send the cookie before send the headers (content-type line).

    Good luck!

    Graciliano M. P.
    "The creativity is the expression of the liberty".

      But what I have noted is that you are not showing your domain in your example.

      I'm using an actual tld in the format '.domain.com'

      The cookie is actually being set successfully, it's retrieving it that is the problem. That would lead me to believe it's a problem with the domain and/or path, but I've quadruple checked them and it all looks right.

      Other thing you need to know, and probably already know, is that you need to send the cookie before send the headers (content-type line).

      I'm currently sending the cookie with the code    print $q->header(-type=>'text/html', -cookie => $cookie); which seems to work. This couldn't be causing a problem, could it?

      Thanks for the help :)

Re: Empty Cookie
by cfreak (Chaplain) on Jun 27, 2003 at 13:18 UTC

    Try changing domain to to the $ENV{HTTP_HOST} variable or setting it explicitly. It has been my personal experience that the '.domain.com' notation described in the CGI docs doesn't work. (If someone knows how to make it work that would be great)

    Lobster Aliens Are attacking the world!

      Hi, thanks for the reply. I tried that but I'm still getting the same error. Thanks anyways though.