|
"100+ Auto-Installing Software Titles For Your Web Site"
|
Name: <INPUT NAME=name VALUE="John"> Gender: <INPUT NAME=gender VALUE="Male"> Age: <INPUT NAME=age VALUE="25">When the above form is submitted, the name-value pairs are sent back to the web server as one long string, which you need to parse. It's not very complicated, and there are plenty of existing routines to do it for you. The long string is in one of these two formats:
#/usr/bin/perl
# parse_get.cgi
&parse_form;
print "Content-type: text/html\n\n";
foreach my $key (sort keys %FORM)
{
print "$key = $FORM{$key}<br />\n";
}
exit;
sub parse_form {
local($name, $value);
# First we split all name-value pairs
foreach (split(/[&;]/, $ENV{'QUERY_STRING'})) {
# Now convert all + signs to spaces
s/\+/ /g;
# Split the name-value pairs between the = signs
# Then assign to local $name and $value
($name, $value)= split('=', $_, 2);
# Convert all hexadecimal characters back to ASCII
$name =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
$value =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/ge;
# Assign the name-value pairs to the global hash %FORM
$FORM{$name} .= "\0" if defined($FORM{$name});
$FORM{$name} .= $value ;
}
}
POST is normally used to send a chunk of data to the server to be processed.
When an HTML form is submitted using POST, your form data is attached to the end of the POST request,
in its own object (specifically, in the message body). This is not as simple as using GET, but is more
versatile. For example, you can send entire files using POST. Also, data size is not limited like it is
with GET. Some advantages of POST are that you're unlimited in the data you can submit, and you can
count on your script being called every time the form is submitted.
#/usr/bin/perl
# parse_form.cgi
&parse_form;
print "Content-type: text/html\n\n";
foreach my $key (sort keys %FORM)
{
print "$key = $FORM{$key}<br />\n";
}
exit;
sub parse_form {
# If it's a GET request use the QUERY_STRING variable
if ("\U$ENV{'REQUEST_METHOD'}\E" eq 'GET') {
# Split the name-value pairs
@pairs = split(/&/, $ENV{'QUERY_STRING'});
}
# If it's a POST request read from STDIN and get the length
# from the CONTENT_LENGTH environment variable
elsif ("\U$ENV{'REQUEST_METHOD'}\E" eq 'POST') {
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
}
else {
# If neither method is called, show an error message
&error('request_method');
}
foreach $pair (@pairs) {
# Split the name-value pairs and assign to $name and $value
($name, $value) = split(/=/, $pair);
# Convert + signs to spaces and hexadecimal characters to ASCII
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# If they try to include server side includes, erase them, so they
# aren't a security risk if the html gets returned. Another
# security hole plugged up.
$value =~ s///g;
# Remove HTML Tags
$value =~ s/<([^>]|\n)*>//g;
# Assign the name-value pairs to the hash %FORM
if ($FORM{$name} && ($value)) {
$FORM{$name} = "$FORM{$name}, $value";
}
elsif ($value ne "") {
$FORM{$name} = $value;
}
}
}
sub error {
local($msg) = @_;
print "Content-Type: text/html\n\n";
print "<CENTER><H2>$msg</H2></CENTER>\n";
exit; }
The above code can be used to display all of the variables of any form
you create on your web site.