获取表单传送数据的子程序一:
sub readPostInput(){
my(%searchField, $buffer, $pair, @pairs);
if($ENV{'REQUEST_METHOD'} eq 'POST'){
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs){
($name, $value) = split(/=/, $pair);
$value =~tr/+//;
$value =~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$name =~tr/+//;
$name =~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$searchField{$name} = $value;
}
}
return (%searchField);
}
在主程序中调用子程序:
%postInputs = readPostInput();
在主程序中调用子程序后,可以使用的变量:
$postInputs{'var'}
获取表单传送数据的子程序二:
my ($data, $i, @data, $key, $val, %FORM);
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$data = $ENV{'QUERY_STRING'};
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$data,$ENV{'CONTENT_LENGTH'});
}
@data = split(/&/,$data);
foreach $i (0 ..$#data) {
# Convert plus's to spaces
$data[$i] =~ s/\+/ /g;
# Split into key and value.
# splits on the first =
($key, $val) = split(/=/,$data[$i],2);
# Convert %XX from hex numbers to alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;
# Kill SSI command
$val =~ s/ //g;
# Associate key and value
# \0 is the multiple separator
$FORM{$key} .= "\0" if (defined($FORM{$key}));
$FORM{$key} .= $val;
}
在主程序中可以使用的调用变量:
$FORM{var}
阅读(1358) | 评论(0) | 转发(0) |