Kid tested. Mother approved.

#!/usr/bin/perl -w use strict; use CGI qw/header cookie start_html end_html url param/; use Digest::MD5 qw(md5_hex); my %login = ( test => 'test' ); # this is just for demo $login{$_} = md5_hex($login{$_}) foreach (keys %login); sub checkSessionID { my $check = shift; my ($user,$pass) = split /:/,$check,2; return $login{$user} eq $pass ? 1:0; } sub checkUserPass { my $user = shift; my $pass = md5_hex(+shift); return $login{$user} eq $pass ? 1:0; } sub makeCookieValue { my $user = shift; my $pass = md5_hex(+shift); return "$user:$pass"; } my %options; my $cookie; # yes, dirty and disgusting...but...oh well if (param('clearCookie')) { $options{"-cookie"} = cookie(-name => 'sessionID', -value => 'bye', -expires => '-6M' # just in case ); } elsif (cookie('sessionID') && checkSessionID(cookie('sessionID'))) { $cookie = cookie(-name => 'sessionID', -value => cookie('sessionID'), -expires => '+1h' ); } elsif (param('username') && param('password') && checkUserPass(param(' +username'),param('password'))) { $cookie = cookie(-name => 'sessionID', -value => makeCookieValue(param('username'),param +('password')), -expires => '+1h' ); } $options{"-cookie"} = $cookie if $cookie; # if you wish, you can put more header options in %options # if you wish to put more cookies, do: # $options{'-cookie'} = [ $options{'-cookie'} ]; # push @$options{'-cookie'}, $_ foreach (@more_cookies); print header(%options),start_html; # $cookie acts as our switch if ($cookie) { print "Logged in and working! Cookie: ",cookie('sessionID') ? cookie +('sessionID'):"You just logged in!"; print "<br /><form action='",url(-absolute=>1),"' method='post'><inp +ut type='hidden' name='clearCookie' value='1'> <input type='Submit'></form>"; } else { print "<form action='",url(-absolute=>1),"' method='post'> Login <input type='text' name='username' value='test'><br /> Pass <input type='text' name='password' value='test'><br /> <input type='Submit'></form>"; } print end_html;

Sorry for sending you on a wild goose chase.

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


In reply to Re: A rotten cookie by antirice
in thread A rotten cookie by sulfericacid

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.