繁体   English   中英

访问多个<select>使用 Perl CGI 的 -ed 参数

[英]Accessing multiple <select>-ed parameters with Perl CGI

我正在使用 Perl CGI模块。 如果我有这样的 HTML

<select multiple name="FILTER_SITE">
  <option>1</option>
  <option>2</option>
</select>

并提交我的表单,我可以在 URL 中得到类似的信息: [..] FILTER_SITE=1&FILTER_SITE=2

Perl 是my $FILTER_SITE = $cgi->param('FILTER_SITE'); 将只捕获第一个实例。

我如何利用两者(在这种情况下)? 破解它并自己解析引用并将它们添加到数组中是我的第一个想法,但它会有点混乱,然后我又几乎不精通 CGI.pm 或 Perl。

有趣的是,使用 Data::Dumper

print "<pre>".Dumper($cgi->param('FILTER_SITE')) . "</pre>";

$VAR1 = '1';
$VAR2 = '2';

注意:当前文档(截至 2020 年 5 月 29 日)表示此方法可能导致安全漏洞。 请在下面检查我的答案。

param方法在标量上下文中提供单个值,在列表上下文中(可能)提供多个值。 在这里阅读。

因此,如果您将代码更改为,例如

my @FILTER_SITE   = $cgi->param('FILTER_SITE');

那么该数组将包含该选项的所有选定值。

如果它更适合您的代码,您也可以编写

for my $FILTER_SITE ($cgi->param('FILTER_SITE')) {
  :
}

我知道这是一个旧帖子,但自从这个问题得到回答后,看起来几乎没有什么变化。 我想发布有关此的最新信息,特别是因为已接受的答案现在被视为安全漏洞。 CGI.pm 文档说

{ Warning - calling param() in list context can lead to vulnerabilities if you do not sanitise user input as it is possible to inject other param keys and values into your code. This is why the multi_param() method exists, to make it clear that a list is being returned, note that param() can still be called in list context and will return a list for back compatibility. Warning - calling param() in list context can lead to vulnerabilities if you do not sanitise user input as it is possible to inject other param keys and values into your code. This is why the multi_param() method exists, to make it clear that a list is being returned, note that param() can still be called in list context and will return a list for back compatibility. }

建议改用$cgi->multi_param方法。

解析值的例子

    #!/usr/bin/perl

    use Encode;

    print "Content-Type: text/html; charset=UTF-8\n\n";

    if($ENV{'REQUEST_METHOD'} eq "POST") {
      read(STDIN, $querystring, $ENV{'CONTENT_LENGTH'});
     print "<h1>POST</h1>";
    } else {
      print "<h1>GET</h1>";
      $type = "display_form";
      $querystring = $ENV{'QUERY_STRING'};
    }

    print "<p>$querystring</p>";

    if (length ($querystring) > 0){
      @pairs = split(/&/, $querystring);
      foreach $pair (@pairs){
           ($name, $value) = split(/=/, $pair);
           $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
           $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
           if (exists $in{$name}) {
             $value = "$value,$in{$name}";
           }
           $in{$name} = $value;
      }
    }

   foreach my $val (sort keys %in) {
     print "<p>$val: $in{$val}</p>";
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM