Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Cookies

by Kiko (Scribe)
on Dec 02, 2000 at 01:05 UTC ( #44473=perlquestion: print w/replies, xml ) Need Help??

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

Hey, I have a simple question. Is it possible to set two cookies at once? + for example: print "Set-Cookie: Name=$user"; print "Set-Cookie: Passwd=$pass"; Thanks, Kiko

Replies are listed 'Best First'.
Re: Cookies
by btrott (Parson) on Dec 02, 2000 at 01:13 UTC
    Sure, but you need to have a carriage return between them:
    print <<HEAD; Set-Cookie: Name=foo Set-Cookie: Passwd=bar HEAD
    And then end your HTTP headers with two carriage returns, per the usual.

    Alternatively you could use CGI::Cookie. From the docs:

    use CGI qw/:standard/; use CGI::Cookie; # Create new cookies and send them $cookie1 = new CGI::Cookie(-name=>'ID', -value=>123456); $cookie2 = new CGI::Cookie(-name=>'preferences', -value=>{ font => Helvetica, size => 12 }); print header(-cookie=>[$cookie1,$cookie2]);
Re: Cookies
by quidity (Pilgrim) on Dec 02, 2000 at 01:23 UTC

    Yes, it is possible and very easy if you use the CGI.pm module (available from CPAN). This is the answer to many questions which arise when writing CGI scripts, so you might want to check it out.

    Briefly, two cookies can be set by following this example(see the CGI.pm documentation for more details):

    # at the start of your script use CGI; my $query = CGI::new(); # then later my $cookie1 = $query->cookie(-name=>'Name', -value=>$user,); my $cookie2 = $query->cookie(-name=>'Passwd', -value=>$pass); print $query->header(-cookie=>[$cookie1,$cookie2]);

    To get cookies back from a user, simply say:

    $user = $query->cookie('Name'); $pass = $query->cookie('Passwd');

    As you seem to want to save the user's name and password to their machine, you might want to rethink your session management. It is a bad thing to do it as you are because another user on the same computer can easily read someone else's cookies, find out their password, and then pretend to be them.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2021-12-08 10:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (34 votes). Check out past polls.

    Notices?