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

Can anyone tell me why the cookie created by this javascript...
<script language=javascript type="text/javascript"> <!-- function SetCookie(username, value, expires, path, domain) { document.cookie = username + "=" + escape(value) + ((expires == null) ? "" : "; expires=" + expires.toGMTString()) + ((path == null) ? "" : "; path=" + path) + ((domain == null) ? "" : "; domain=" + domain); } var expiration = new Date(); expiration.setTime(expiration.getTime() + 60000); SetCookie('username', 'Peter', expiration); // --> </script>
is not seen by this perl script? The cookie shows on my hard drive so i know it's being created.
#!/usr/local/bin/perl use CGI; $q = new CGI; print $q->header; $cookie_in = $q->cookie("username"); if($cookie_in) { print $cookie_in; } else { print "Can't find cookie\n"; }
-Lisa.

Replies are listed 'Best First'.
Re: javascript.. perl.. cookie
by antirice (Priest) on Oct 31, 2003 at 23:45 UTC

    Hmmm. Fairly good question why it's not working for you. I tried your javascript and it worked in both Mozilla and IE. The only reason I can think of for the cookie's refusal to show is that the page that contains the javascript is under a path different from the perl script. When I changed the path of the html page from that of the script, it did fail. The obvious solution is to set the path to the deepest common path. Of course, this may simply be '/'.

    Hope this helps.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1

      are you saying you were able to get the perl script to read the javascript cookie? -Lisa

        Yes, I was able to get the perl script to read the javascript cookie. However, the path had to be set when the perl script and the html page didn't have the same path. If you don't specify the path when the cookie is set by javascript, the current path is used. Since you didn't set it (SetCookie takes up to five params yet you only provide three, the fourth would be the path) it uses the current path. If the path of the perl script isn't below said path, the browser will not send the cookie.

        For illustrative purposes:

        These work: HTML at: http://www.blah.com/page.html Perl script at: http://www.blah.com/cookie.pl HTML at: http://www.blah.com/page.html Perl script at: http://www.blah.com/cgi-bin/cookie.pl This doesn't work: HTML at: http://www.blah.com/content/page.html Perl script at: http://www.blah.com/cookie.pl

        Of course if the perl script is generating the html content, the cookie should be picked up the next time you visit the script.

        Hope this helps.

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

Re: javascript.. perl.. cookie
by injunjoel (Priest) on Nov 01, 2003 at 00:59 UTC
    Greetings,
    Im unable to reproduce your problem. I would suggest looking into using CGI::Cookie for cookie parsing especially if you are unsure of the source of those cookies. Also Mozilla(netscape) has a really handy cookie manager so you can see if your cookies are being set and how.
    Here is how I tested it (and I got something back).
    #!/usr/bin/perl -w use CGI; use CGI::Cookie; use strict; use Dumpvalue; my %cookies = fetch CGI::Cookie; my $q = new CGI; if(%cookies){ dump_ref(\%cookies); }else{ my $this_url = $ENV{'SCRIPT_NAME'}; print $q->header(); ################################################# #pasted code from posting ################################################# print qq* <script language=javascript type="text/javascript"> <!-- function SetCookie(username, value, expires, path, domain){ document.cookie = username + "=" + escape(value) + ((expires == null) ? "" : "; expires=" + expires.toGMTString() +) + ((path == null) ? "" : "; path=" + path) + ((domain == null) ? "" : "; domain=" + domain); } var expiration = new Date(); expiration.setTime(expiration.getTime() + 60000); SetCookie('username', 'Peter', expiration); // --> </script> <br> <a href="$this_url">Check cookies</a> *; exit; } ################################################# #what do we have in there? ################################################# sub dump_ref { my $ref = shift; my $dumper = new Dumpvalue; print $q->header(); print "<pre>"; $dumper->dumpValues($ref); print "</pre>"; exit; }
    simple I know but I hope that helps.
    I was wondering about your line
    var expiration = new Date(); expiration.setTime(expiration.getTime() + 60000);
    was expiration suppose to be expires?
    -injunjoel
Re: javascript.. perl.. cookie
by shenme (Priest) on Oct 31, 2003 at 23:08 UTC
    Not being a cookie monkster myself I'm not sure, but does the browser know to return the cookie to the www host?   That is, how does the domain and path information get filled in when you don't specify anything?   If the cookie isn't associated with a particular domain, will it be returned to all domains?   Just wondering...

    Update:   I'm starting to find references that the domain defaults to the "domain of the document".   I _suppose_ that's believable ;-)

Re: javascript.. perl.. cookie
by Cody Pendant (Prior) on Nov 01, 2003 at 04:30 UTC
    Can you tell us what you're trying to do, Lisa?

    Are you setting the cookie and trying to retrieve it, or are you using LWP or whatever to go to a website and get the cookies it sets?

    More detail would help.



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
Re: javascript.. perl.. cookie
by Anonymous Monk on Nov 01, 2003 at 01:16 UTC
    How do cookies work? Once you can answer this question you can begin to debug why your perl script can't see the cookie.