Ok this is how I did it. Note I'm very new to perl programming so this probably isn't the best way.
Assuming you have authenticated the user (from a database or text file or where-ever), and $user is the user's id
use MD5 ; my $md5 = new MD5 ; $md5->reset ; my $yday = (localtime)[7]; # create certificate / session id my $certif = $user . $yday . "do4k.g0" . $ENV{'HTTP_USER_AGENT'} . +$ENV{'REMOTE_ADDR'} ; # encrypt certificate $md5->add($certif); my $enc_cert = $md5->hexdigest() ; # set cookie print "Set-Cookie: SESSION=$enc_cert; path=/\n" ; print "Set-Cookie: NAME=$user; path=/\n" ; # and continue print "Content-type: text/html\n\n" ; print "Your logged In!" ;
Then everytime the script is called get the certificate out the cookie and recreate a certificate and compare the two.
# $session and $user came from cookie use MD5 ; my $md5 = new MD5 ; $md5->reset ; #create ceritficate my $yday = (localtime)[7]; my $certif = $username . $yday . do4k.g0 . $ENV{'HTTP_USER_AGENT'} . + $ENV{'REMOTE_ADDR'} ; # encrypt Certificate $md5->add($certif); my $enc_cert = $md5->hexdigest() ; #compare if($enc_cert eq $session) { # we're logged in - run script ; } else { # we're not logged in - disp error msg }
And a logout can simply be done with a
print<<"END" ; Set-Cookie: SESSION=; path=\ Set-Cookie: NAME=; path=\ Content-type: text/html Your logged out now END
It would probably be wise to set expiration times for the cookies. Using the $yday means each certificate will expire at midnight which could be a problem.

Anyway I hope this helps
Nomis52


In reply to Re: Re: Re: Is this use of crypt() appropriate? by Nomis52
in thread Is this use of crypt() appropriate? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.