1

このような CGI スクリプトがあります。

#!/usr/local/bin/perl

use CGI ':standard';

print header;
print start_html('A Simple Example'),
h1('A Simple Example'),
start_form,
"What's your name? ",textfield('name'),
p, submit, end_form,
hr;

my %unwantedwords = {'foo' => 1 };

if (param())
{
    my $text =param('name');

    # I attempted this to but failed.
    unless ($unwantedwords{$text}){
        print  "Your name is: ",$text,
   }
    hr;
}
print
end_html;

私がやりたいことは、基本的に「textfield」を介してテキストを受け取り、それを Web 上に印刷することです。しかし、ユーザーによって挿入された単語が不要な単語 (ハッシュに格納されている) である場合、それを印刷する代わりに、Web を新しい初期状態に戻してほしいと思います。

それを行う最良の方法は何ですか?上記のコードは機能しません。

4

2 に答える 2

1

(未テスト)のようなもの..

use strict;
use warnings;
use CGI qw( :standard );
use CGI::Carp qw( fatalsToBrowser );

my @unwanted = qw( foo bar baz );

my $text = param('name');

print header,
      start_html('A Simple Example');

display_form() and exit unless !grep($text eq $_, @unwanted);

print "Hello $text\n";

sub display_form {
   print start_form,
         h1('A Simple Example'),
         qq( What's your name? ), textfield(-name => 'name', -value => '', -override => 1), p,
         submit, hr,
         end_form;
}

print end_html;
于 2013-06-11T02:01:25.460 に答える
0

送信する前に単語の状態を保存し、送信した単語が不適切な単語のリストに含まれている場合は、それを取得して返送する必要があります。

実装は永続化エンジンによって異なりますが、Cookie を使用して古い単語を保存するか、セッション ストアを使用するかに関係なく、次のようにします。

 1. store the old word

 2. send the old word along with the web form.

 3. receive new word back

 4. if (new word is in the bad word list) {
      get the old word from storage
    }
    else {
      store the new word
    }

 5. do what comes next
于 2013-06-10T14:27:51.783 に答える