簡體   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