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

I have a problem with getting/setting cookies. When I use CGI::Cookie, it works w/Opera, but not with Firefox or IE. The code I am using is:
$main::session = new CGI; $main::cookies = $main::session->cookie(-name => 'NCWiFiTest', -domain => 'localhost', -path => '/', #-expires => '+1d', -value => `date` ); print $main::session->header(-cookie=>$main::cookies); #%main::test = fetch CGI::Cookie; $main::test = ""; print "<hr>\n"; $main::query = new CGI; $main::test = $main::query->cookie(-name => 'NCWiFiTest'); #$main::cookies->bake; #print "Content-type: text/html\n\n"; #print "<br>\n"; print "NCWiFiTest --> $main::test\n";
While looking at the headers in FF, the "Set-Cookie" never seems to be sent, while in Opera it works correctly. Warnings and strict are set, with no errors or warnings being reported. CGI.pm is 3.29, Perl is 5.8.8. Thanks in advance

Replies are listed 'Best First'.
Re: CGI::Cookie problem in FF and IE
by proceng (Scribe) on Aug 07, 2007 at 02:53 UTC
    Update: Here is the entire code:
    #!/usr/bin/env perl use strict; use warnings; #BEGIN { # $| = 1; # open (STDERR, ">&STDOUT"); # print qq~Content-type: text/html\n\n~; #} use CGI; use CGI::Cookie; use CGI::Carp qw(fatalsToBrowser); use DBI; use DBD::Pg; use LWP 5.69; use HTTP::Request::Common qw( POST ); use HTML::Entities; my $buffer = ""; my $name = ""; my $value = ""; my @pair; my $key; my $cookie; my $test; my $session; my $query; #$main::cookies = $main::session->cookie(-name => 'NCWiFiTest', # Set username $main::session = new CGI; $main::cookie = $main::session->cookie(-name => 'User-Name', -value => 'todd', -domain => 'localhost', -path => '/', -expires => '+1d' ); $main::cookie->bake; print "<hr>\n"; $main::query = new CGI; $main::test = ""; $main::test = $main::query->cookie(-name => 'User-Name'); print "User Name --> $main::test\n"; print "<br>"; print "<hr>\n"; foreach $main::key (keys %ENV) { print "$main::key --> $ENV{$main::key}<br>"; } print "End of environment<P>"; # Get the input read(STDIN, $main::buffer, $ENV{'CONTENT_LENGTH'}); # Split the name-value pairs @main::pairs = split(/&/, $main::buffer); foreach $main::pair (@main::pairs) { ($main::name, $main::value) = split(/=/, $main::pair); # Un-Webify plus signs and %-encoding $main::value =~ tr/+/ /; $main::value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # Stop people from using subshells to execute commands # Not a big deal when using sendmail, but very important # when using UCB mail (aka mailx). $main::value =~ s/~!/ ~!/g; # Uncomment for debugging purposes print "Setting $main::name to $main::value<P>"; } exit;
    The call returns (in opera) with the following values set:
    HTTP_COOKIE --> User-Name=todd
    HTTP_COOKIE2 --> $Version=1

    Live HTTP Headers (in Firefox) shows:
    Set-Cookie: User-Name=todd; domain=localhost; path=/; expires=Wed, 08-Aug-2007 02:49:15 GMT Update: I tried using CGI::Simple::Cookie. This works, the only change noted is that the header includes "Cookie: " instead of "Set-Cookie: ". I will investigate this further.

      Hello proceng,

      Welcome to the Monastery. I took a look at your code and I notice that actually the complete version you posted is quite different from the smaller section you posted in your original question, so I'll try to respond to both.

      As for your original question, my best guess as to why the cookie is not appearing comes from using the Unix command date to generate your query value. This string will have a newline on the end of it which may confuse your browser. One way to handle this, if that's really the value you want to use, is

      my $value = `date`; chomp $value;

      However, in the larger update you posted, the code appears to be working correctly. The code generates a cookie called "User-Name" and that is indeed what the browser shows.

        The problem that I have seen (the date command was simply to create changed data on each iteration w/o changing the code) is that the cookie does not show up in the "show cookies" list and is not retrieved in either FF or IE. With your suggestion, the newline is not transferred, but the cookie still is not set in FF even though it is sent as shown in the HTTP header.
      abandon that un-webify stuf and use CGI;