here you are, also are running apache server with perl
#!c:/perl/bin/perl.exe -w
$cfh = select (STDOUT);
$| = 1;
select ($cfh);
#the above line is to provide the scripts interpreter location--------
+>scripts: login.pl
#---------------------------------------session_start.pl
#---------------------------------------#include the libraries:
use CGI qw/:standard/; # load standard CGI routines
use CGI; #load the object orentird CGI
use DBI; # load DBI library
use CGI::Session; # load the Session library
use CGI::Carp qw(fatalsToBrowser); # load CGI-Carp for browser er
+rors handling
my $query = new CGI;
#---------------------------------------#
if( $query->param("Submit") ){
our $message;
#-------check the login
if ($query->param("login")) {
$login1 = $query->param("login");
unless ($login1 =~ /^[a-z0-9_]+$/) {
$login = '';
$message .= 'The login ID contains invalid character!<br>';
}else{ $login = $query->param("login") }
}else{
my $login = '';
$message .= 'You forget to enter your login ID!<br>';
}
#-------check the password
if ($query->param("password")) {
$password1 = $query->param("password");
unless ($password1 =~ /^[a-z0-9_]+$/) {
$password = '';
$message .= 'The password contains invalid character!<br>';
}else{ $password = $query->param("password") }
}else{
my $password = '';
$message .= 'You forget to enter your password!<br>';
}
if ($login && $password) {
#----------------------
my $sqlstatement="bla bla";
my $sth = $dbh->prepare($sqlstatement);
$sth->execute || die "Could not execute SQL statement ... maybe invali
+d? $DBI::errstr";
my @row=$sth->fetchrow_array;
if (@row) {
our $first_name = $row[0];
our $last_name = $row[1];
our $user_id = $row[2];
our $admin = $row[3];
our $user_address = $row[4];
#----------create the user session
my $session = new CGI::Session("driver:File", $query, {'Directory' =>
+"C:\\session\\"} ) or die "S!";
$session->header();
my $session_id = $session->id(); #get the session I
+D
$session->param('username', $username); #add the username
+to the session
$session->param('user_id', $user_id); #add the user_id t
+o the session
$session->param('first_name', $first_name); #add the first_nam
+e to the session
$session->param('last_name', $last_name); #add the last_name
+ to the session
$session->param('address', $user_address); #add the address t
+o the session
$session->param('admin', $admin); #add the admin ide
+ntity to the session
#save session params in the $query object
$session->expire('+10m'); #session should e
+xpire after 10 minutes
$cookie = $query->cookie(CGISESSID => $session->id);
print $query->header( -cookie=>$cookie );
print $query->header(-type => 'text/html', -cookie=>$cookie, -location
+=>'http://localhost/cgi-bin/cars/loggedin.pl' );
exit;
#-------------- finish MySQL statement
$sth->finish();
#-------------- disconnect from database
$dbh->disconnect();
}else{ $message .= 'The username and the password do not match <br>t
+hose in the record.<br>'; }
}else{ $message .= 'Try again.'; }
} #---------the param bracket
|