如何重定向到CGI中的HTML页面

时间:2016-12-03 17:50:24

标签: html perl

我正在使用两个文件进行Perl和HTML编程:mainpage.htmlmainpage.cgi。我希望让用户在文本框中输入用户名和密码,如果其中任何一个错误,页面应保持不变,让用户再次输入。

mainpage.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>User Login</title>
  </head>
  <body>
    <form action="http://localhost/cgi-bin/mainpage.cgi" method="post">
      Username: <input type="text" name="Username">  <br />
      Password: <input type="text" name="Password" />
      <input type="submit" value="submit" />
    </form>
  </body>
</html>

mainpage.cgi

#!c:/Perl64/bin/perl.exe

local ( $buffer, @pairs, $pair, $Username, $Password, %FORM );

# Read information
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;

if ( $ENV{'REQUEST_METHOD'} eq "POST" ) {
    read( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} );
}
else {
    $buffer = $ENV{'QUERY_STRING'};
}

@pairs = split( /&/, $buffer );

foreach $pair ( @pairs ) {
    ( $Username, $Password ) = split( /=/, $pair );
    $Password =~ tr/+/ /;
    $Password =~ s/%(..)/pack("C", hex($1))/eg;
    $FORM{$Username} = $Password;
}
$Username = $FORM{Username};
$Password = $FORM{Password};

#Database
use DBI;
$dbh = DBI->connect( 'dbi:mysql:math_questions', 'root', 'admin' )
        or die "Connection Error: $DBI::errstr\n";

$sql = "select * FROM users where Username = '$Username' and Password = '$Password'";
$sth = $dbh->prepare( $sql );
$sth->execute or die "SQL Error: $DBI::errstr\n";
while ( @row = $sth->fetchrow_array ) {
    $user = $row[0];
    $key  = $row[1];
}

if ( $user ) {
    print "Content-type:text/html\r\n\r\n";
    print "<html>";
    print "<head>";
    print '<meta charset="utf-8">';
    print '<title>math_questions</title>';
    print "</head>";
    print "<body>";
    print "<h2>Correct</h2>";
    print "</body>";
    print "</html>";
}
else {
    #I don't know how to do this part in a good way, thank you very much!
}

1;

1 个答案:

答案 0 :(得分:-2)

print "Content-type:text/html\r\n\r\n";
open(DATA, '<invaliduser.html') or die "invaliduser.html  $!"; 
while(<DATA>){
    print "$_";
}