使用CGI脚本进行Perl表单验证

使用CGI脚本进行Perl表单验证,perl,validation,cgi,Perl,Validation,Cgi,我正在努力完成我的作业的最后一项任务,就是在将表单提交给另一个CGI程序之前验证表单 我有一个简单的CGI程序,它会要求用户输入数据 #!/usr/bin/perl -w use CGI qw/:standard/; # Standard HTTP header print header(); # Write information to data file and produce a form &printForm(); # Finish HTML page print end

我正在努力完成我的作业的最后一项任务,就是在将表单提交给另一个CGI程序之前验证表单

我有一个简单的CGI程序,它会要求用户输入数据

#!/usr/bin/perl -w

use CGI qw/:standard/;

# Standard HTTP header
print header();

# Write information to data file and produce a form
&printForm();

# Finish HTML page
print end_html();

# This sub will create a form to access the print_fortune.cgi script
sub printForm
{
        print qq~

<html>
<head><title>My Search Engine</title>
</head>

<body>
  <form action="b1.cgi" method="GET">
        What is your e-msil address? <input type="text" name="passing" size=40>
        <input type="submit" value="send address">
        <input type="hidden" name="form" value="insert" />
        </form>

<form method="get" action="b1.cgi" enctype="application/x-www-form-urlencoded">

<input type="text" name="search" value="" size="30" /><br />

<label><input type="radio" name="option" value="name" checked="checked" />name</label>

<label><input type="radio" name="option" value="author" />author</label><label>

<input type="radio" name="option" value="url" />url</label>

<label><input type="radio" name="option" value="keyword" />keyword</label>

<input type="submit" name=".submit" value="Search" />
<input type="hidden" name="passing" value="http://default.com" />

<div><input type="hidden" name="form" value="search"  /></div></form>


</body>
所以上面的程序包含两种形式。一个是向数据库中添加新数据,另一个是从数据库中进行搜索

#!/usr/bin/perl

print "Content-type: text/html\n\n";

use LWP::Simple;
use CGI;
use HTML::HeadParser;
use DBI;

my $serverName = "";
my $serverPort = "";

my $serverUser = "";
my $serverPass = "";
my $serverDb   = "";

my $serverTabl = "";

$cgi = CGI->new;

my $pass = $cgi->param('passing');

$URL = get ("$pass");

$head = HTML::HeadParser->new;

$head->parse("$URL");

my $methods = $cgi->param('form');


if ($methods eq "insert"){

insert_entry();

}

show_entries();

sub insert_entry {
    my ($dbh, $success, $name, $author, $url,$temp);

    $dbh = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort",$serverUser,$serverPass);
    $name = $head->header('X-Meta-Name');
    $author = $head->header('X-Meta-Author');
    $url = $cgi->param('passing');
    $temp = $head->header('X-Meta-Keywords');
    @keyword = split(/,/,$temp);


    $success = $dbh->do("INSERT INTO $serverTabl(name,author,url,keyword1,keyword2,keyword3,keyword4,keyword5) VALUES(?,?,?,?,?,?,?,?)", undef,$name,$
author,$url,$keyword[0],$keyword[1],$keyword[2],$keyword[3],$keyword[4]);
    $dbh->disconnect;
    if($success != 1) {
       return "Sorry, the database was unable to add your entry.
                                Please try again later.";
    } else {
        return;
      }
}

sub show_entries {
    my ($dbh, $sth, @row);
    my $search = $cgi->param('search');
    my $option = $cgi->param('option');

    $dbh = DBI->connect("DBI:mysql:database=$serverDb;host=$serverName;port=$serverPort",$serverUser,$serverPass);

    $sth = $dbh->prepare("SELECT *
                          FROM $serverTabl
                          WHERE $option LIKE '%$search%'");
    $sth->execute;
    print "Existing Entries",HR;
    while(@row = $sth->fetchrow_array) {
          $row[5] = scalar(localtime($row[5]));
          print "<table border='2'><tr>";
          print "<td>" .  $row[0] . "</td>";
          print "<td>Name" . $row[1] . "</td>";
          print "<td>Author" . $row[2] . "</td>";
          print "<td>URL" . $row[3] . "</td>";
          print "<td>Keyword1" . $row[4] . "</td>";
          print "<td>Keyword2" . $row[5] . "</td>";
          print "<td>Keyword3" . $row[6] . "</td>";
          print "<td>Keyword4" . $row[7] . "</td>";
          print "<td>Keyword5" . $row[8] . "</td>";
          print "</tr></table>";
     }
     $sth->finish;
     $dbh->disconnect;
}
现在的问题是,在表单提交到第二个程序之前,我如何为表单提交创建正则表达式

我想为你做验证

名称允许空格,但仅允许字母字符 作者允许空格,但仅允许字母字符 关键字不允许空格,只允许字母字符 url仅允许字母数字字符,并且下列字符:/。~?=+&不能连续存在两个句点

我真的很抱歉,但我对Perl真的很陌生。我们只学过PHP,但Perl几乎什么都没有……

Perl文档列出了所有\p正则表达式属性

对于只包含字母的字符串,您需要

/^[\p{Alpha}]+$/
对于只包含所需字母和空格的字符串

/^[\p{Alpha}\x20]+$/
要匹配URL,的文档将此作为匹配URL的正式模式

m|^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?$|
一定要在你的作品中引用参考文献以获得额外的分数