Cookie-based Authentication: Login program - preCharge Forums
It shows that you are unregistered. Please register with us by clicking Here
preCharge Forums


Nav Green LeftNav Right
preCharge Forums > Website Design & Development > Programming > Perl » Cookie-based Authentication: Login program


Reply
Tcat Right
 
LinkBack Thread Tools Display Modes Tcat Right
Old 08-19-2006   #1 (permalink)
Ankita
Member
 
Join Date: Jul 2006
Age: 25
Posts: 53
Default Cookie-based Authentication: Login program

#!/usr/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use lib '.';
use users;
use strict;

my $user = param('username');
my $pass = param('password');
my $username = "";

my $sth = $dbh->prepare("select * from users where username=?") or &dbdie;
$sth->execute($user);
if (my $rec = $sth->fetchrow_hashref) {
my $salt = substr($rec->{password}, 0, 2);
if ($rec->{password} ne crypt($pass, $salt) ) {
&dienice(qq(You entered the wrong password. If you've forgotten your password, <a href="forgotpass.html">Click here to reset it</a>.));
}
$username = $rec->{username};
} else {
&dienice("Username <b>$user</b> does not exist.");
}
my $cookie_id = &random_id;
my $cookie = cookie(-name=>'cid', -value=>$cookie_id, -expires=>'+7d');

$sth = $dbh->prepare("replace into user_cookies values(?, ?, current_timestamp(), ?)") or &dbdie;
$sth->execute($cookie_id, $username, $ENV{REMOTE_ADDR}) or &dbdie;

if (param('page')) {
my $url = param('page');
# CGI.pm's redirect function can accept all of the same parameters
# as the header function, so we can set a cookie and issue a redirect
# at the same time.
print redirect(-location=>"http://www.cgi101.com/$url", -cookie=>$cookie);
} else {
# no page was specified, so print a "you have logged in" message.
# On a production site, you may want to change this to print
# a redirect to your home page...
print header(-cookie=>$cookie);
print start_html("Logged In");
print qq(<h2>Welcome</h2>\nYou're logged in as <b>$username</b>!<br>\n);
print qq(<a href="securepage.cgi">go to secure page</a><br>\n);
print qq(<a href="logout.cgi">log out</a><br>\n);
print end_html;
}

sub random_id {
# This routine generates a 32-character random string
# out of letters and numbers.
my $rid = "";
my $alphas = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTUVWXYZ";
my @alphary = split(//, $alphas);
foreach my $i (1..32) {
my $letter = $alphary[int(rand(@alphary))];
$rid .= $letter;
}
return $rid;
}
Ankita is offline   Reply With Quote


Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Ads under login bar Cholo Graphics & Multimedia 6 08-11-2006 01:26 AM
Prev and Next links based on rows in database destiny ASP 1 07-14-2006 08:44 PM
Looking to buy web-based business/ technology andrew General Business Issues 0 07-04-2006 05:56 PM
login Jake All Things General 2 06-20-2006 06:47 PM
Nintendo based skin Daphnes Graphics & Multimedia 29 03-27-2006 10:46 PM


footer left
All times are GMT. The time now is 03:42 AM.

DISCLAIMER: preCharge Risk Management is not responsible for any opinions, advice or comments expressed on the preCharge Community Forums.
preCharge® is a registered trademark of preCharge Risk Management | chargeback protection | Merchant Account Blog

Powered by vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC6

Home Loan | Advertising | Best Credit Cards | Mobile Phones | Mortgages

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49