如何解析和回显CGI查询字符串参数?

时间:2010-02-10 15:33:56

标签: perl cgi

我想使用URL中的系统请求参数在服务器上执行命令。我正在运行服务器。

在浏览器中 - >> //localhost:9009/?comd显示我放在代码中的目录中的文件列表

if (/comd/i )        { print  $client `dir`;      }

我如何解析请求参数?例如:

http://localhost:9009/?comd&user=kkc&mail=kkc@kkc.com

我想返回 - >> hello user please confirm your email kkc@kkc.com

如何解析网址中的请求参数?

3 个答案:

答案 0 :(得分:3)


use CGI;

my $cgi = CGI->new();

my $user = $cgi->param( "user" );
my $mail = $cgi->param( "mail" );

print( "Hello $user please confirm your email $mail\n" );

答案 1 :(得分:3)

#!/usr/bin/perl
$|++;                              # auto flush output

use CGI;                           # module that simplifies grabbing parameters from query string
use HTML::Entities;                # for character encoding
use strict;                        # to make sure your Perl code is well formed

print qq{Content-type: text/html\n\n};  # http header b/c outputting to browser

   main();                         # calling the function

   sub main{
      my $cgi    = new CGI;        # store all the cgi variables
      # the following stores all the query string variables into a hash
      #   this is unique because you might have multiple query string values for
      #   for a variable. 
      #   (eg http://localhost/?email=a@b.com&email=b@c.com&email=c@d.com )
      my %params = map { $_ => HTML::Entities::encode(join("; ", split("\0", $cgi->Vars->{$_}))) } $cgi->param;

      #Input: http://localhost:9009/?comd&user=kkc&mail=kkc@kkc.com
      #Output: Hello kcc please confirm your email kkc@kkc.com

      print <<HTML;
      <html>
         <head>
            <style type="text/css">.bold{font-weight:bold}</style>
         </head>
         <body>
            Hello <span class="bold">$params{user}</span> please confirm your email <span class="bold">$params{mail}</span>
         </body>
      </html>
HTML

      return 1;
   }

我希望这会有所帮助。

答案 2 :(得分:2)

use CGI;
use HTML::Template;

my $cgi = CGI->new;

my $html = qq{
    <!DOCTYPE HTML>
    <html>
    <head><title>Confirmation</title></head>
    <body><p>Hello <TMPL_VAR USER ESCAPE=HTML>. 
    Please confirm your email <TMPL_VAR MAIL ESCAPE=HTML></p></body>
    </html>
};

my $tmpl = HTML::Template->new(scalarref => \$html, associate => $cgi);
print $cgi->header('text/html'), $tmpl->output;