Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Cookie not being set...

by memnoch (Scribe)
on Mar 13, 2008 at 17:24 UTC ( #674022=perlquestion: print w/replies, xml ) Need Help??

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:

  • As far as I can tell, my browser (IE6) isn't restricting cookies. After browsing the web for even a few minutes, my cookie folder is loaded with cookies without any trouble or warnings.
  • If I clear all cookies and then run my script, no new cookies are created.
  • If I execute my code on the command line, it appears to be creating the expected cookie header.
  • The url I use to access the script is: http://localhost/cgi-bin/cookie_test.pl. (I've also tried http://127.0.0.1/cgi-bin/cookie_test.pl but get same results.)
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...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://674022]
Approved by Erez
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2022-06-28 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (90 votes). Check out past polls.

    Notices?