Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Setting single cookie with a separator

by htmanning (Friar)
on Aug 25, 2011 at 01:57 UTC ( [id://922249]=perlquestion: print w/replies, xml ) Need Help??

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

Monks, I'm currently setting 3 cookies for a site login. It works fine, but not on a BlackBerry Browser. There's a bug where the BB wants 1 cookie only. So, I need to set a single cookie and separate the values, then read it back in. Currently I'm setting them like this:
sub write_cookies { print "Set-Cookie: reguser=$username; expires=$expires; path=/;\n" +; print "Set-Cookie: regpass=$password; expires=$expires; path=/;\n" +; print "Set-Cookie: regtype=$regtype; expires=$expires; path=/;\n"; }
Then I read it like this:
local(@getcookies) = split (/; /,$ENV{'HTTP_COOKIE'}); local(%cookies); foreach(@getcookies){ ($ckey, $cval) = split (/=/,$_); $cookies{$ckey} = $cval; } $username = $cookies{'reguser'}; $password = $cookies{'regpass'}; $regtype = $cookies{'regtype'}; $login = "Login" if ($username && $password && !$logout);
I need to instead set 1 cookie, and then separate the values when I read it back in. Any help would be appreciated. Thanks.

Replies are listed 'Best First'.
Re: Setting single cookie with a separator
by Anonymous Monk on Aug 25, 2011 at 02:33 UTC
    First try CGI::Cookie before deciding BlackBerry Browser is broken
      Okay, is there something wrong with the way I'm doing it now? The BB is the only browser that can't handle it. When trying to fetch them like this, it doesn't work. What am I missing?
      %cookies = CGI::Cookie->fetch; $username = $cookies{'reguser'}->value; $password = $cookies{'regpass'}->value; $regtype = $cookies{'regtype'}->value;

        Okay, is there something wrong with the way I'm doing it now?

        If you have to ask, its safe to assume the answer is yes :)

        your posted code doesn't do any form of encoding/escaping,

        use CGI or die;

        use CGI or die;

Re: Setting single cookie with a separator
by scorpio17 (Canon) on Aug 25, 2011 at 13:05 UTC

    Never store a password inside a cookie!

    All you need in the cookie is a unique session id. Use this id to access associated data on the server.

      Exactly! A cookie has no security; thats why I store my passwords in an egg

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-23 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found