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

I have two simple scripts one to set and one to get the cookie. The script uns fine under IE but under Netscape it does not set the cookie. But other cookies are being set on the netscape browser that I am running by others. Is there a difference between IE and Netscape on this ???
################################## #set cookie ################################## #!/usr/bin/perl -w use CGI ; my $q = new CGI; my $co_userid = $q->cookie(-name=>'user', -value=>'dummy', -domain=> '.jplan.com', -path=> '/cgi-bin/') ; my $co_password = $q->cookie(-name=>'password', -value=>'hispassword', -domain=> '.jplan.com', -path=> '/cgi-bin/'); print $q->header(-cookie=>[$co_userid,$co_password]); print "<html>\n"; print "<HEAD>","\n"; print "<TITLE>CTB Discussion Index</TITLE>\n"; print "<link rel=stylesheet type=\"text/css\" href=\"../../ctb +/css/header.css\">\n"; print "</head>\n"; print "<body bgcolor=#FFFFFF topmargin=2 marginheight=2 leftma +rgin=2 marginwidth=2 >\n"; print "Cookie is set<br>"; print "</body></html>"; ####################################### #get cookie ####################################### #!/usr/bin/perl -w use CGI; $query = new CGI; my $ci_password = $query->cookie('user'); my $ci_userid = $query->cookie('password'); print "Content-type: text/html\n\n"; print "<html>\n"; print "<HEAD>","\n"; print "<TITLE>Messages Posted Last 10 Days -- Compact Tractor Board</T +ITLE>","\n"; print "<link rel=stylesheet type=\"text/css\" href=\"../../ctb/css/hea +der.css\">\n"; print "</head>\n"; print "<body bgcolor=#FFFFFF topmargin=2 marginheight=2 leftmargin=2 m +arginwidth=2 >"; print " userid= $ci_userid<br>"; print " pw = $ci_password<br>"; print "</body></html>";

Replies are listed 'Best First'.
Re: Problem with Cookies on Netscape but Ok in IE
by belg4mit (Prior) on Jan 16, 2002 at 06:30 UTC
    Off the cuff my hunch is the leading . you're supplying in the domain. You might want to check the Netscape developer docs, as they are the ones to have originally defined what a cookie is.

    UPDATE A paper on cookies. Note the domain, not the cookie, has . # restrictions. Also note that that leading . would cause problems for people visting your site (assuming you have DNS entires for them) as jplan.com and not www.jplan.com and foo.jplan.com

    --
    perl -pe "s/\b;([st])/'\1/mg"

      I tried without the .domain.com and it still does not work. the code for these to programs can be run:
      http://jplan.com/cgi-bin/ctb/setCookie.pl http://jplan.com/cgi-bin/ctb/getCookie.pl
        Well that setCookie is still setting the domain with a preceding . You are also limiting access to the cookie to only setCookie by the path you are using.
        HTTP/1.1 200 OK Date: Wed, 16 Jan 2002 03:09:40 GMT Server: Apache/1.3.19 (Unix) FrontPage/4.0.4.3 Set-Cookie: user=dummy; domain=.jplan.com; path=/cgi-bin/ctb/setCookie +.pl Set-Cookie: password=hispassword; domain=.jplan.com; path=/cgi-bin/ctb +/setCookie.pl Connection: close Content-Type: text/html

        --
        perl -pe "s/\b;([st])/'\1/mg"

        I wrote a client side program to write the cookie, and it succeeds, and my cgi get cookie works so the setCookie cgi program is still the problem. ummmm..........
Re: Problem with Cookies on Netscape but Ok in IE
by Hero Zzyzzx (Curate) on Jan 16, 2002 at 08:31 UTC

    Please tell me that you aren't really going to use cookies to store usernames and passwords. . .

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

      Oh yes, good one - and I was going to add, why are you using CGI but not using it?
      #!/usr/bin/perl -w use CGI ; my $q = new CGI; my $co_userid = $q->cookie(-name=>'user', -value=>'dummy', -domain=> '.jplan.com', -path=> '/cgi-bin/') ; my $co_password = $q->cookie(-name=>'password', -value=>'hispassword', -domain=> '.jplan.com', -path=> '/cgi-bin/'); print $q->header(-cookie=>[$co_userid,$co_password]), $q->start_html({-title => 'CTB Discussion Index', -style => {'src'=>'../../ctb/css/header.c +ss'}, -bgcolor => '#FFFFFF', -topmargin => 2, -marginheight => 2, -leftmargin => 2, -marginwidth => 2), $q->p('Cookie is set'), $q->br, $q->end_html;
      As for the password, I'd set a session ID instead, that expires after a certain length of time (store it in a text file or DB somewhere for later checking).

      cLive ;-)

Re: Problem with Cookies on Netscape but Ok in IE
by dereks (Scribe) on Jan 16, 2002 at 21:35 UTC
    I've had problems with cookies when a path is set, so you may try not defining a path. It's not required anyway, unless you plan on having many cookies from different parts of your site. Also, if that doesn't work, try it without the domain as well. I removed both on my local machine and it worked on NS just fine.

    - Derek Soviak

      I found some code on this site and it solves my problem writes in bothe netscape and IE finally !!!!!!
      #!/usr/bin/perl -w use CGI; use CGI::Carp (fatalsToBrowser); use CGI::Cookie $co = new CGI; $cook = $co->cookie( -name=>'ctbuser', -path=>'/', -expires=>'+1h', -value=>'pixxadonut' ); print $co->header(-cookie=>$cook); print 'ok you set my cookie pal';
Re: Problem with Cookies on Netscape but Ok in IE
by davis (Vicar) on Jan 16, 2002 at 14:37 UTC
    Hi there,
    A couple of notes:
    Have you tried the "warn before accepting a cookie" option in Netscape - it's often useful when debugging cookie problems?
    In the header() function in CGI.pm, you could use the function "cookies" rather than "cookie" - I'm 98 percent certain that one is just the same as the other, but it's a style note.
    davis
      This code is sample code it has nothing to do with what I am actually doing with cookies so don't worry about the password. And Yes I have been using the Netscape cookie notify when accepting cookies from the begining. I get the warning for everyone but me. The activestate Perl docs state for the domain:
      "2. domain This is a partial or complete domain name for which the cookie is vali +d. The browser will return the cookie to any host that matches the pa +rtial domain name. For example, if you specify a domain name of ``.ca +pricorn.com'', then Netscape will return the cookie to Web servers ru +nning on any of the machines ``www.capricorn.com'', ``ftp.capricorn.c +om'', ``feckless.capricorn.com'', etc. Domain names must contain at l +east two periods to prevent attempts to match on top level domains li +ke ``.edu''. If no domain is specified, then the browser will only re +turn the cookie to servers on the host the cookie originated from.
      So it looks liek I am following the spec, but I am going to eliminate both of these. I also will try the all CGI module version I think Netscape does not like the hybrid approach, little touchy you know! Thanks for the imput this has been driving me nutso!!!! Dennis