Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Serializing Cookies ???

by lindex (Friar)
on Dec 06, 2000 at 02:37 UTC ( #45107=perlquestion: print w/replies, xml ) Need Help??
lindex has asked for the wisdom of the Perl Monks concerning the following question:

Ok so Iam working on this small mod_perl project. This project requires me to use cookies to maintain session info, but very small session info such as username,password,time of login. now usally I would do this by just creating a string with a delimiter and using that as the cookies value. But as I was sitting there I realized it would be really cool if I just serialized an array as my cookie value and then just used it on every sessions use. Now mind you I havent had much use for serializing data in perl so I might be wroung in the entire idea but here is some basic code to give an example as to what Iam doing.
use Storable qw(nfreeze thaw); sub make_cookie { return($q->cookie( -name=>'sessionID', -value=>unpack("H*",nfreeze(['1245','foo','foopass'])), -expire=>'+10m', -secure=>0 )); } sub see_cookie { return(@{thaw(pack("H*",$_[0]))}); }
I think that this idea should work, but Iam getting an error from storable saying "Magic number checking on storable string failed etc etc ..". would anyone care to shed a little light on my situation?

/****************************/, /*****************************/

Replies are listed 'Best First'.
Re: Serializing Cookies ???
by steveAZ98 (Monk) on Dec 06, 2000 at 03:04 UTC
    This code seems to work for me if I change from nfreeze to freeze as in the code below. I'm not sure what your error means, I was unable to duplicate it. Here are two scripts that I put together to test these routines.
    #!/usr/bin/perl -w use CGI; use Storable qw(freeze); my $q = CGI->new(); sub make_cookie { $q->cookie( -name=>'Test', -value=>unpack("H*",freeze(['1245','foo','foopass'])), -path=>'/' ); } print $q->header(-cookie=>make_cookie()); print $q->start_html(-title=>'Test cookie'); print "Cookie Set<br>"; print $q->end_html;
    and this to retrieve the cookie.
    #!/usr/bin/perl -w use CGI; use Storable qw(thaw); my $q = CGI->new(); sub see_cookie { @{ thaw( pack("H*",$_[0]) ) }; } print $q->header(); print $q->start_html(-title=>'Test cookie'); print "Cookie Value: ", (join " -- ", see_cookie($q->cookie('Test')) ) +, "<br>"; print $q->end_html;
    I tried these scripts under mod_perl also and they work in that enviorment also (Apache::Registry), I would take a look at your Storable module.
      Hmm maybe its the install of Storable on my machine, or maybe mod_perl has issues with Storable or vice versa?

      /****************************/, /*****************************/
Re: Serializing Cookies ???
by lhoward (Vicar) on Dec 06, 2000 at 03:23 UTC
    The docs from CGI::Cookie indicate that it already supports automatic serilization of simple datasturctures:
    ... The value can be a scalar, an array reference, or a hash reference. (At some point in the future cookies will support one of the Perl object serialization protocols for full generality).
    use CGI::Cookie; $cookie2 = new CGI::Cookie(-name=>'preferences', -value=>{ font => Helvetica, size => 12 } print header(-cookie=>[$cookie2]);
(Ovid) Re: Serializing Cookies ???
by Ovid (Cardinal) on Dec 06, 2000 at 02:47 UTC
    I can't comment on your actual problem, but I thought I should point out that using cookies to store password information is not a very secure practice. You can check out for some examples of how easy it is to snatch passwords this way. Also, are you running on a secure server? If not, then you're sending the password out every time the cookie is set, thus increasing the vulnerability. Since you are sending the username WITH the password, you're handing over the keys to the kingdom.


    Update: lindex has a good point. Sometimes this isn't an issue.

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      this issue of security is very minor in this script. Iam well aware of the risks of storing passwords in formdata or cookies, But all the authinication tables in sql (oracle) are controled from a linked access database that is for all intent world readable+writeable, So I just do what my boss tells me and try and have fun with my code along the way :)
      plus the network is all internal
      the ppl using it dont have access to the "int0rnet soopa hIway" :)

      /****************************/, /*****************************/

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://45107]
Approved by root
[jdporter]: Pict, Copt, what's the difference.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2017-04-24 21:37 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (447 votes). Check out past polls.