HTML代码:
<html>
<head>
<title>Fugo</title>
</head>
<body>
<form action="cgi-bin/env_var.cgi" method="get">
<input type="text" name="username" ><br />
<input type="password" name ="password"><br />
<input type="submit" value="LOGIN">
</form>
</body>
</html>
CGI代码:
#!/usr/bin/perl
print "Content-type:text/html\r\n\r\n";
print '<html>';
print '<head>';
print '<meta charset="utf-8">';
print '<title>Enviroment variables</title>';
print '</head>';
print '<body>';
print '<pre><code>';
local ($query_string, @pair, $name, $value, %form);
#foreach (sort keys %ENV){
# if ($_ eq "QUERY_STRING"){
# print "<b>$_</b>:$ENV{$_}\n";
# }
#}
print "$ENV{'REQUEST_METHOD'}\n";
$query_string = "$ENV{'QUERY_STRING'}\n";
@pairs = split(/&/, $query_string);
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value=~ s/%(..)/pack("C", hex($1))/eg;
#注意,这里必须将'\n'去掉。
chomp $value;
$form{$name} = $value;
}
my $host = "localhost";
my $port = "3306";
my $database = "fugo";
my $dsn = "DBI:mysql:database=$database:$host:$port";
my $dbh = DBI->connect($dsn, "root", "123456") or die $DBI::errstr;
my $sth = $dbh->prepare("select username , phone_no from client where username = ? and password = sha1(?)");
$sth->execute($form{username}, $form{password});
my @row = $sth->fetchrow_array();
if (@row){
print "Login successfully.";
}
else{
print "Login failed.";
}
print '</code></pre>';
print '</body>';
print '</html>';
1;