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

Dear Monks,

This is a newbie cookie question. I have done a web search on this, but have not found anything. Any help would be appreciated.

I'm trying to set cookies from a CGI script, but they don't get set on the filesystem. Here are some important points:

Here is the output of running my script on the command line:
C:\Apache2\cgi-bin>perl cookie_test.pl Set-Cookie: login=o21PmKkYHxpCVIrPGlqaXZWIMqyLv11vJhMGEFEtdmvNRfFXXySd +0zvZxOU7U8Cg; path=/ Date: Thu, 13 Mar 2008 17:21:28 GMT Content-Type: text/html; charset=ISO-8859-1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> <h2>Cookie Page</h2> </body> </html>
And here is my code:
#!c:/perl/bin/perl.exe use CGI qw(:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); use strict; use warnings 'all'; my $query = CGI->new(); my $cookie = &make_cookie; print $query->header(-cookie => $cookie), $query->start_html, "<h2>Coo +kie Page</h2>", $query->end_html; ################################################## sub make_cookie { my $cookie_id = &make_random_64_id; my $cookie = cookie(-name => 'login', -value => $cookie_id, -path +=> '/'); # Expires when browser is closed return $cookie; } ################################################## # Make random cookie ID from 64 random alphanumeric characters. sub make_random_64_id { my $rand_id; my @alpha_nums = ('a'..'z', 'A'..'Z', '0'..'9'); for (1..64) { $rand_id .= $alpha_nums[int(rand(@alpha_nums))]; } return $rand_id; }
Am I doing something wrong in the way that I set the cookie, or is this possibly an environmental or browser problem?

Update: I also just tried adding the following line:

127.0.0.1    www.mylocalhost.com

to the C:\WINDOWS\system32\drivers\etc\hosts file, and then used the following url:

http://www.mylocalhost.com/cgi-bin/cookie_test.pl

The script diplays in the browser fine, but still no cookie is created.

Another update...think I have the answer: I just tried using Firefox off my flash drive (we can't install it on our machines here), and the cookie gets created....so it seems like this may be an IE issue.

Replies are listed 'Best First'.
Re: Cookie not being set...
by Thilosophy (Curate) on Mar 14, 2008 at 07:13 UTC
    I'm trying to set cookies from a CGI script, but they don't get set on the filesystem.

    Unless you specify that you want a persistent cookie, there is no reason why the browser should save it to the filesystem. A session cookie could (should?) be kept entirely in memory.

    The script diplays in the browser fine, but still no cookie is created.

    Can you verify that the cookie gets sent back to the server with the next request, or that it is accessible by JavaScript? If so, your cookie should be okay.

Re: Cookie not being set...
by Anonymous Monk on Mar 14, 2008 at 04:30 UTC
    IE has complex cookie policy settings...