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

Okay I'm having a heck of a time getting a cookie to set in Internet Explorer (the problem seems to occur on any version). I'm using CGI.pm to call the CGI::Cookie routines and I read somewhere that older versions of CGI.pm didn't do cookies correctly on IE so I upgraded to the latest version. Anyway, I know I've gotten this to work before. This isn't the actual code I'm working with but it does the same thing and gives the same error. (domain names were changed to protect the innocent)

#!/usr/bin/perl use CGI; use strict; my $cgi = new CGI; my %foo = (key1=>"value1",key2=>"value2"); my %from_cookie = $cgi->cookie(-name=>'foo'); if(%from_cookie) { print "Content-type:text/html\n\n"; print "<html><body>\n"; foreach my $key(keys %from_cookie) { print "$key = [$from_cookie{$key}]<br>\n"; } print "</body></html>\n"; } else { my $cookie = $cgi->cookie( -name=>'foo', -value=>\%foo, -expires=>'+1h', -path=>'/', -domain=>'somedomain.com', ); print $cgi->header(-cookie=>$cookie); print "<html><body>I have set the cookie</body></html>\n"; } exit;

The program I'm working on uses the hash as the cookie as this one does, but I also tested this just passing a single value to the cookie and got the same result: The cookie sets and works just fine on Netscape and Mozilla but on IE it doesn't set. I know I'm probably doing something dumb :) but this one just has me stumped.

Thanks in advance

Chris

Replies are listed 'Best First'.
Re: Evil Cookies in IE
by chipmunk (Parson) on Nov 09, 2001 at 21:00 UTC
    This is the only thing I can think of, and it actually involves the domain name, so I don't know if it even applies to your real code.

    In your example, you've got -domain=>'somedomain.com',. The CGI documentation states:

    2. a domain This is a partial or complete domain name for which the cookie is valid. The browser will return the cookie to any host that matches the partial domain name. For example, if you specify a domain name of ".capricorn.com", then the browser will return the cookie to Web servers running on any of the machines "www.capricorn.com", "www2.capricorn.com", "feckless.capricorn.com", etc. Domain names must contain at least two periods to prevent attempts to match on top level domains like ".edu". If no domain is specified, then the browser will only return the cookie to servers on the host the cookie originated from.
    And I seem to recall having problems setting cookies in my own scripts, when I had specified the domain with a single period. So, try changing your code to -domain=>'.somedomain.com', and see if that fixes the problem.
      Yeah, I actually have a sub domain in front of it as in: mymachine.somedomain.com.
        If it is truly a "subdomain" then you need a dot in front of it. If it is a hostname, then that format is likely to work. Just for kicks, try ".somedomain.com" and see if it works.

        HTH, --traveler