Perl CGI表格重用以前的数据

时间:2012-01-26 16:09:32

标签: perl forms cgi

我正在使用Perl(使用CGI模块)和recaptcha构建一个注册表单。表单工作正常,并将数据提交到SQL数据库。但是,当我使用表单创建另一个用户时,输入数据库的数据与第一个用户相同。我正在使用my $var = $cgi->param('param_name');在我的验证页面中检索表单数据。我是否需要清除参数,或者是其他内容。 (我尝试了$cgi->delete_all();,但似乎没有做任何事情)

表单验证码:(它实际上是原型,因此安全性尚未得到解决)

my $challenge = $q->param('recaptcha_challenge_field');
my $response = $q->param('recaptcha_response_field');
my $username = $q->param('Username');
my $password = $q->param('Password');
my $name = $q->param('Name');
my $email = $q->param('Username');
my $security = $q->param('Security');
my $answer = $q->param('Answer');
my $permissions = 1;

# Verify submission
my $result = $c->check_answer(
    "my_private_key", $ENV{'REMOTE_ADDR'},
    $challenge, $response
);

if ( $result->{is_valid} ) {
    insert_new_user();
    print  $q->redirect('cgi-bin/admin/text_campaign.pl');
}
else {
    # Error
    print  $q->redirect('login.pl?crc=false');
}

###############################################################################
# Sub Routines                                                                #
###############################################################################
sub insert_new_user
{
    my $sql = "INSERT INTO users (u_username, u_password, u_realname, u_email, u_security_question, u_security_answer, PRIVILEGES_idPRIVILEGES)
               VALUES(?, ?, ?, ?, ?, ?, ?) ";
    my $sth=$dbh->prepare($sql);
    $sth->execute($username, $password, $name, $email, $security, $answer, $permissions);
    $sth->finish();

    return;
}

1 个答案:

答案 0 :(得分:0)

是的,这就是它通常的工作方式。查看文档中的-nosticky“pragma”

http://perldoc.perl.org/CGI.html#PRAGMAS

或delete_all()函数。

---编辑---

我使用CGI.pm文档中的示例的修改形式播放了一些内容。包括它在这里是为了便于参考,因为我改变了一点。

#!/usr/bin/perl

use CGI qw/-nosticky :standard/;

print header;
print start_html("Example CGI.pm Form");
print "<h1> Example CGI.pm Form</h1>\n";
do_work();
print_prompt();
print_tail();
print end_html;

sub print_prompt {
    print "<hr>\n";
    print start_form;
    print "<em>What's your name?</em><br>";
    print textfield('name');
    print checkbox('Not my real name');
    print "<p><em>Where can you find English Sparrows?</em><br>";
    print checkbox_group(
                            -name=>'Sparrow locations',
                            -values=>[England,France,Spain,Asia,Hoboken],
                            -linebreak=>'yes',
                            -defaults=>[England,Asia]);
    print "<p><em>How far can they fly?</em><br>",
        radio_group(
                -name=>'how far',
                -values=>['10 ft','1 mile','10 miles','real far'],
                -default=>'1 mile');
    print "<p><em>What's your favorite color?</em>  ";
    print popup_menu(-name=>'Color',
                            -values=>['black','brown','red','yellow'],
                            -default=>'red');
    print hidden('Reference','Monty Python and the Holy Grail');
    print "<p><em>What have you got there?</em><br>";
    print scrolling_list(
                    -name=>'possessions',
                    -values=>['A Coconut','A Grail','An Icon',
                            'A Sword','A Ticket'],
                    -size=>5,
                    -multiple=>'true');
    print "<p><em>Any parting comments?</em><br>";
    print textarea(-name=>'Comments',
                            -rows=>10,
                            -columns=>50);
    print "<p>",reset;
    print submit('Action','Shout');
    print submit('Action','Scream');
    print end_form;
    print "<hr>\n";
}

sub do_work {
    print "<h2>Here are the current settings in this form</h2>";
    for my $key (param) {
        print "<strong>$key</strong> -> ";
        my @values = param($key);
        print join(", ",@values),"<br>\n";
    }
}

sub print_tail {
    print <<END;
<hr>
<address>Lincoln D. Stein</address><br>
<a href="/">Home Page</a>
END
}

保持原样,此脚本展示了我们正在讨论的行为。 -nosticky的使用似乎没有帮助。

但是,如果我在do_work之后和print_prompt()之前添加Delete_all,就像这样:

print header;
print start_html("Example CGI.pm Form");
print "<h1> Example CGI.pm Form</h1>\n";
do_work();
Delete_all();
print_prompt();
print_tail();
print end_html;

然后默认值不会预先填充。

我希望这会有所帮助。