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

I'm having trouble fetching a cookie that I set. I used this code to set the cookie...

my $c = new CGI::Cookie( -name => 'perl_timesheet_username', -value => 'hello');

$html->PrintHTML("Set-Cookie: $c");

Set Set-Cookie does print the exact parameters I submitted, however, this code for fetching the data afterwards does not pull anything and also I checked the cookie folder and there is no new cookie...

my (%cookies) = fetch CGI::Cookie;
foreach (keys %cookies)
{
$html->PrintHTML("Cookie - $cookies{$_}");
}

This prints nothing. Am I doing something wrong? I pulled this information right from the perlmonks man page for CGI.pm for setting and fetching cookies.

Oh yeah, I'm using NT 4.0 WKSTN with PWS.

Thanks.

Replies are listed 'Best First'.
Re: Fetching cookies.
by btrott (Parson) on Apr 20, 2000 at 10:20 UTC
    Perhaps this is obvious, but the Set-Cookie directive has to be part of your HTTP header... is that how you're using it? I don't know what your $html->PrintHTML statements are doing. If you're using CGI.pm to print out your HTML and your header, do something like this:
    my $query = new CGI; print $query->header(-cookie => $c);
    This will print out the entire HTTP header, including the Set-Cookie directive w/ the correct values.

    If you're using mod_perl, do something like this:

    $r->content_type("text/html"); $r->header_out("Set-Cookie", $c); $r->send_http_header;
    where $r is the Apache::Request object that you received in your handler routine.

    Does this help?

RE: Fetching cookies.
by Anonymous Monk on Apr 20, 2000 at 20:35 UTC
    For setting a cookie I used
    print $mycgi->header(-cookie=>$params->cookie(-name=>'sessionID', -value=>'some stuff', -expires=>'0', -path=>'/'));
    and for reading it: <CODE> $mycgi->cookie(-name=>'sessionID'); <CODE>

    CGI.pm's object interface really makes code cleaner. One assumes that the function oriented way you did it SHOULD work.

    There are a bunch of other pitfalls with cookies though. One of the simplest ones to have is the "path problem". Netscape may not think it SHOULD return the cookie, because it may think its talking to a different web server.

    In HTTP/1.1 the client sends a header that tells the web server what domain it is requesting a response from. The web server may be configured (IE apache "ServerName" directive) to return a response telling the browser the response came from a DIFFERENT domain, as in client asks for a page from www.rhombus.net, and apache (happily misconfigured) responds with an HTTP response telling the client the response came from barney.rhombus.net.

    In that case your cookies WILL NOT WORK. Netscape or IE will figure that the cookie is intended for www.rhombus.net and the next time you ask for a page netscape will try to go to barney.rhombus.net, and so it won't send back the cookie, so no cookie value! Or alternatively your "path" parameter for the cookie may be specifying a domain that isn't the one apache is set up to respond as, results are the same.

    In framesets this gets especially ugly. Browsers often don't seem to grok where content is really coming from. Telnetting to port 80 is highly useful sometimes...