Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Trouble writing a cookie with CGI::Session

by djw (Vicar)
on Aug 05, 2003 at 16:14 UTC ( [id://281050]=perlquestion: print w/replies, xml ) Need Help??

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

I'm playing around with CGI::Session (using MySQL as the driver) and I'm having a problem with the cookie end of things. The session gets created and stores the information into the database, but for some reason, it's not saving a cookie file to my local browsers settings (IE).

In the CGI::Session Tutorial documentation, it says to write a cookie like so:
$cookie = $cgi->cookie(CGISESSID => $session->id); print $cgi->header( -cookie=>$cookie );

I read somewhere that a cookie won't be written locally unless " -path => '' " isn't set when creating your cookie (read this on another forum, may not be correct). Looking at CGI's documentation, I see that a cookie would be invoked like this:
$cookie = $query->cookie(-name => 'sessionID', -value => 'xyzzy', -expires => '+1h', -path => '/cgi-bin/database', -domain => '.capricorn.org', -secure => 1); print $query->header(-cookie=>$cookie);


How do I work with the CGI object so that I can use CGI::Session and also write the cookie to the browser? I seem to be able to only do one or the other (I can write the cookie, but then my session doesn't seem to work, or I can get the session working, but the cookie doesn't get written).

"My" code is just the examples from the CGI::Session Cookbook at this point - I'm just trying to get it to work.

djw

Replies are listed 'Best First'.
Re: Trouble writing a cookie with CGI::Session
by talexb (Chancellor) on Aug 05, 2003 at 16:23 UTC

    You need to send the HTTP header using the CGI::Session method 'header'. Note that the documentation may state incorrectly that

    $session->header;
    will output the header, complete with CGI::Session cookie information. That should actually be
    print $session->header;
    Yes, I wrote to the authour and he wrote back to confirm this mistake.

    --t. alex
    Life is short: get busy!
Re: Trouble writing a cookie with CGI::Session
by antirice (Priest) on Aug 05, 2003 at 16:37 UTC

    In the documentation, they have the following:

    $cookie = new CGI::Cookie(-name=>'CGISESSID', -value=>$session->id); print $cgi->header(-cookie=>$cookie);

    Of course you can just use cookie(-name... and all will be well. The syntax you used produces a cookie as such:

    CGISESSID=whatever-it-is; path = /

    Which has no expiration and as such expires immediately. I personally don't like using print $session->header since you can only use one cookie and it specifies the type. 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://281050]
Approved by talexb
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-19 14:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found