http://qs321.pair.com?node_id=39159

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

I've been using the standard method for setting cookies using perl/cgi:
$logincookie = $co->cookie ( -name=>'ST', -value=>'test', -expires=>'+30d' -path=>'/', -domain=>'www.strategictechnology.net', -secure=>0 ); print $co->header(-cookie=>$logincookie);
And I'm trying to read the cookie using javascript (via document.cookie), but although it is clear that the cookie is set (looking at the magic cookie file), a javascript function can't read it (document.cookie is empty). What am I doing wrong?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How can I get Javascript to read CGI-generated cookies
by r34d0nl1 (Pilgrim) on Dec 14, 2004 at 13:43 UTC
    The standard mantra should work (as explained in perldoc CGI) when dealing with cookies.
    Quoted from perldoc is an explanation to -path option:

    "3. a path If you provide a cookie path attribute, the browser will check
    it against your script's URL before returning the cookie.
    For example, if you specify the path "/cgi-bin", then the cookie will be returned to each of
    the scripts "/cgi-bin/tally.pl", "/cgi-bin/order.pl", and "/cgi-bin/customer_service/complain.pl",
    but not to the script "/cgi-private/site_admin.pl". By default, path is set to "/", which causes
    the cookie to be sent to any CGI script on your site."

    I think that you first should try to read the values of your cookie to check if it can be read by another perl page. If it is Ok, then you should not worry about the cookie generation, but worry about your JavaScript code...
Re: How can I get Javascript to read CGI-generated cookies
by Trimbach (Curate) on Oct 30, 2000 at 23:36 UTC
    If the cookie is properly set, then your problem isn't with Perl.

    So how do you read a cookie using Javascript? Beats me. I would try a Javascript forum to see if you can get answers there. (I like to use http://www.deja.com to search the Usenet postings.)

    Originally posted as a Categorized Answer.

Re: How can I get Javascript to read CGI-generated cookies
by michellem (Friar) on Oct 31, 2000 at 10:56 UTC
    I think the problem is with the 'path' attribute. When I look at the cookie, the path appears as /cgi-bin/ which of course makes sense, but I'm not sure how to make sure that the path get's set as '/', even though I've specified it in the script. Should I set an absolute path?
Re: How can I get Javascript to read CGI-generated cookies
by little (Curate) on Oct 31, 2000 at 13:58 UTC
    cookies are stored in the clients browser cache, so there won't be anything like cgi-bin. read the docs for cookies. Cause javascripts can read in fact through a bug nearly all files on your hard drive but shouldn't. cookies get only sent back to whom created them, that is the server. So what kind o cookie do you have to set, that a javascript would be allowed to access (referring to docs)?

    Originally posted as a Categorized Answer.