1. 程式人生 > >Get方法獲取url引數

Get方法獲取url引數

GET方法的引數記錄在REQUEST_METHOD環境變數裡

示例:

環境:

perl 5.20.2 

apache(httpd2.4.16)

#!i:/Perl64/bin/Perl.exe

local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET")
{
    $buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
    # print "<h4>$pair</h4>";
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    # http://localhost:8081/cgi-bin/test-00-09.cgi?first_name=xx%20m%20cf&last_name=xxmcf524
    # $value = xx%20m%20cf
    $value =~ s/%(..)/pack("C", hex($1))/eg;
    # $value = xxm m cf
    $FORM{$name} = $value;
}
# key 名為 "first_name"的鍵
$first_name = $FORM{first_name};
$last_name  = $FORM{last_name};
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print "<title>Hello - Second CGI Program</title>";
print "</head>";
print "<body>";
print "<br><h3>buffer:$buffer</h3>";
print "<h2>Hello $first_name $last_name - Second CGI Program</h2>";
print "</body>";
print "</html>";
輸出: