You're being a little inconsistent in your code postings. For example is the cookie name 'id' or 'login'?
Why are you using fetchrow instead of fetchrow_array? The former is an old alias for the later and even was removed from the documentation. To learn of it, you need to search the module's source code or have old documentation.
Why are you assigning $x but never use it? BTW, the author recommends against calling fetchrow (or fetchrow_array) in scalar context. You should call it in list context. And most importantly, why aren't you using the strict and warnings pragmas?
Here's an untested rewrite of your 2 scripts which should get you closer to your goal.
Edit: oops - I made a couple updates to the posted script (i.e., I moved the db stuff to a sub) and poj spotted a goof on my part in the placement of $welcome and $login var assignments. Should be fixed now.
home.pl
use strict;
use warnings;
use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $cgi = CGI->new();
my $username = $cgi->param('username');
my $password = $cgi->param('password');
my $id = get_uid($username, $password);
my $welcome = 'http://localhost/cgi-bin/welcome.pl';
my $login = 'http://localhost/cgi-bin/login.pl';
if ($id) {
my $cookie = $cgi->cookie(-name => 'login', -value => $username);
print $cgi->redirect(-location => $welcome, -cookie => $cookie);
}
else {
print $cgi->redirect($login);
}
exit;
sub get_uid {
my $username = shift;
my $password = shift;
my $db = 'datab';
my $usr = 'root';
my $pwd = '';
my $host = 'localhost';
my $dbh = DBI->connect("DBI:mysql:$db:$host", $usr, $pwd,
{AutoCommit => 0, RaiseError => 1})
or die $DBI::errstr;
my $sth = $dbh->prepare("SELECT id
FROM mysql_auth
WHERE username = ? AND password = ?");
$sth->execute($username, $password);
my ($id) = $sth->fetchrow_array;
$sth->finish();
return $id;
}
welcome.pl use strict;
use warnings;
use DBI;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $cgi = CGI->new();
my $sid = $cgi->cookie('login');
my $login = 'http://localhost/cgi-bin/login.pl';
if ( ! $sid ){ # exit and return to login.pl! we have no cookies
print $cgi->redirect($login);
}
#start using the page! we have cookies
print $cgi->header();
# etc
|