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

Hi,

I am using Javascript to set a Cookie and trying to read the cookie back using a Perl script on another page. The reason that I'm using JS is that I wish to allow a user to dynamically add things to a cookie on different pages for a shopping basket and then allow the user to download all the items off a single page.

I am setting the cookie in JS using the document.cookie method as follows:

var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : ""); document.cookie = curCookie;
where the cookie name is cookie and attempting to read the cookie using CGI.pm:
my $cookie_in = $query->cookie("cookie");
but it is reporting that no cookie is set. The cookie set and read scripts are in the same domain and from my reading of cookie standards, there should be no compatibility issues between Perl and JS.

I'd appreciate any help,

Regards,

Kakaze

Replies are listed 'Best First'.
Re: Perl reading Cookies Set by Javascript
by broquaint (Abbot) on Jul 25, 2002 at 11:43 UTC
    Firstly are you sure the cookie is being set in JS? If not the problem could lie in the expires variable which will need to be a Date object as toGMTString() is a method of that class. Once you're sure that the cookie is being set correctly in JS then try the perl again. Failing that, debug debug debug[1]!
    HTH

    _________
    broquaint

    [1]where alert() is JS's equivalent of print() (assuming you're using client-side JS)

      Unfortunately I need to use JS to set the cookie because it is built up of dynamic elements on the page.

      The Cookie is definitely being set and I am aware that JS cookies may be incompatible (from reading the docs of some of the cookie libs out there).

      I did not set the expires variable because I want the cookie to be only available for the session. I'll try setting the expire option.

      Does JS not follow the correct cookie standard? I would imagine that it should and that if it did, Perl should be able to correctly see the cookie.

        Cookies are a browser thing really, there is no standard how to implement them. It's even more vage for javascript cookies.

        For debugging, check if you cookie is visible to javascript (a reload after you set it):

        alert(document.cookie);

        In your script, check if the cookie is available in the environment:

        print $ENV{'HTTP_COOKIE'};

        If the last example does display the cookie, something might be wrong with CGI.pm. An old version maybe?

Re: Perl reading Cookies Set by Javascript
by io (Scribe) on Jul 25, 2002 at 11:38 UTC
    CGI.pm should be able to read your cookie. But it doesn't. Remember that not all browsers support javascript, cookie or setting cookies with javascript. Which is bad.

    Best thing would probably be setting your cookies with CGI.pm, which has less a chance to fail.

Re: Perl reading Cookies Set by Javascript
by dws (Chancellor) on Jul 25, 2002 at 16:41 UTC
    I've had mixed luck setting cookies without first specifying a patch. Try changing   ((path) ? "; path=" + path : "") + to something like   ("path=" + (path) ? path : "/") +