2012-08-12 72 views
3

我試圖爲我的任務實現最後一項任務是在將表單提交給另一個CGI程序之前驗證表單。使用CGI腳本對Perl表單進行驗證

發生什麼事是,我有一個簡單的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; 
} 

所以現在的問題是如何才能在表單提交正式表達式進入第二個程序之前?

我要爲做驗證

允許使用空格,但隻字母字符 作者允許使用空格,但隻字母字符 關鍵字允許沒有空格,只有字母字符 網址只允許字母數字字符和以下內容:/。〜?= + &連續不存在兩個句點。

我真的很抱歉,但我對Perl真的很陌生。我們只被教過關於PHP,但Perl幾乎沒有任何東西......

回答

2

perluniprops Perl文檔列出了所有\p正則表達式屬性。

對於僅包含一串字母,你想

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

對於僅包含字母和空格的字符串你想

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

要匹配的URL URI module的文檔提供了這作爲官方模式匹配一​​個URL

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

請務必引用您工作中的參考資料以獲得額外的分數!