【问题标题】:Perl Form Validation using CGI scripting使用 CGI 脚本的 Perl 表单验证
【发布时间】:2012-08-09 05:27:43
【问题描述】:

我正在尝试完成我的作业的最后一项任务,即在将表单提交到另一个 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;
}

所以现在的问题是如何在表单提交到第二个程序之前为它做一个正则表达式?

我想验证

name 允许使用空格,但只能使用字母字符 author 允许空格,但只能使用字母字符 keywords 不允许有空格,只能使用字母字符 url 只允许字母数字字符和以下 :/.~?=+& 两个句点不能连续存在。

我真的很抱歉,但我对 Perl 真的很陌生。我们只学过 PHP,但 Perl 几乎一无所获....

【问题讨论】:

    标签: perl validation cgi


    【解决方案1】:

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

    对于只包含字母的字符串,你想要

    /^[\p{Alpha}]+$/
    

    对于只包含你想要的字母和空格的字符串

    /^[\p{Alpha}\x20]+$/
    

    为了匹配一个 URL,URI module 的文档将其作为 official 模式来匹配一个 URL

    m|^(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?$|
    

    一定要在你的作品中引用参考文献以获得额外的分数!

    【讨论】:

      猜你喜欢
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      • 2012-01-19
      • 2014-04-21
      相关资源
      最近更新 更多