#!/usr/bin/perl # pizza6.cgi # handles pizza6.html order form - multiple form and page example # get form data $input=untaint(); # Read from stdin and untaint it chomp($input); chomp($input); # Get rid of trailing CR/LF # Check for unencoded equal signs. If there are none, the input # didn't come from a FORM. if ( $input !~ /=/ ) { die "Query String not from FORM\n"; } # Query string okay. Split data into name=value fields, at the ampersand @fields = split ("&", $input); # Process each field and put it into an associative array @toppings = (); # toppings is a multiple choice foreach $one (@fields) { ($name, $value) = split("=",$one); # split at the equal sign &convert($name); # decode the name and value strings &convert($value); if ($name ne "toppings") { $array{"$name"} = $value; # put data into associative array } else { push(@toppings, "$value"); # collect toppings } } # Check for null fields if ($array{"name"} eq "") { &error ("Name field is NULL"); } if ($array{"address"} eq "") { &error ("Address field is NULL"); } # Send the response page # Fixed data is sent using HERE documents # HTTP header must end with a single blank line print < Your Order

Purple Pizza Order Confirmation

Thank you for ordering from Dr. Hwang\'s Purple Pizza Parlor.

EndOfHTMLHeader # Send the variable data print "

You have ordered a ", $array{"size"}, " pizza with "; if (scalar(@toppings) == 0) { $toppinglist=""; print "no toppings\n"; } else { $toppinglist=join(", ", @toppings); print $toppinglist, " as toppings\n"; } print "

\n"; print "to be delivered to:

\n"; print $array{"name"}, "
\n"; # Split address into lines and print each one @lines = split ("\n", $array{"address"}); foreach $line (@lines) { print $line, "
\n"; } print "

\n"; print "
\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "

If this is correct, click here:  \n"; print "

\n"; print "
\n"; print "
\n"; print "

If this is not correct, click here:  "; print "

\n"; print "
\n"; # Send end of document print < EndOfHTMLFooter # Get around security issues sub untaint { $_[0]=~/\A(.*)\Z/m; $_[0]=$1; return $1; } sub convert { $_[0] =~ s/\+/ /g; # Convert + to space $_[0] =~ s/%(..)/pack("c",hex($1))/ge; # Convert URL hex to Latin-1 } sub error { # Send error page and exit print < Error In Form Submission

Error in Form Submission

The following error was detected:

EndOfHTMLHeader # Send the variable data print "

", $_[0], "

\n\n"; # Send end of document print <Please use the Back button to correct your form and resubmit

EndOfHTMLFooter exit 0; }