DelphiFAQ Home Search:
General :: Programming :: perl
General questions about perl.


This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.
Recommended links on this topic:
Featured Article

Code snippets: parsing GET or POST data

The following routine is my standard snippet to parse transmitted data (either GET or POST, not both).

sub parse_form {
    # Get the input
    if ($ENV{'REQUEST_METHOD'} eq 'GET') {
       # Split the name-value pairs
       @pairs = split(/&/, $ENV{'QUERY_STRING'});
    elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
       # Get the input
       read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
       # Split the name-value pairs
       @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;
       # If they try to include server side includes, erase them, so they
       # arent a security risk if the html gets returned.  Another
       # security hole plugged up.
       $value =~ s/<!--(.|\n)*-->//g;
       $FORM{$name} = $value;

Generated 4:01:26 on Dec 7, 2019