Zerlegung von FORM-Variablen in einem Perl-CGI-Skript
Wie erhält man die Original-Variablen eines Formulars in einem
CGI-Skript?
Hier ein Beispiel:
Das umsetzende Perl-Programm sieht (z.B.) so aus:
#!/usr/local/bin/perl -w
# parsing environment variables for POST and GET CGI scripts
# (c) mg sai uni ulm 1998
print "Content-type: text/html\n\n";
print "<html><title>Printing CGI-FORM-Variables</title><body><p>\n";
print "<h1><hr><center>Printing CGI-FORM-Variables</center></h1><hr>\n";
getvars(); # getting all Form-Vars
foreach $var (sort keys %FORM) {
print "$var=$FORM{$var}<br>\n";
}
# saving all CGI-Variables to a hash named FORM
sub getvars {
my $line;
my $pair;
my $name;
my $value;
if (!exists $ENV{"REQUEST_METHOD"}) {
print "use program in CGI context!\n";
print "environment variable REUQEST_METHOD is undefined\n";
exit;
}
if ($ENV{"REQUEST_METHOD"}=="GET") { # GET-Method
$line = $ENV{"QUERY_STRING"};
}
else { # POST-Method
# reading x bytes from stdin
read(STDIN, $line, $ENV{'CONTENT_LENGTH'});
}
print "<br>original line is: >$line<<br><br>\n";
my @args = split(/&/, $line); # splitting line by &
foreach $pair (@args) # Process the name=value argument pairs
{ ($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /; # Unescape the argument value
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/chr(hex($1))/eg;
# /e means: eval right side as expr
# chr converts the ascii value into the corespondant character
$FORM{$name} = $value; # Save the name=value pair
}
}