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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |