Perl读取CGI环境变量

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值