CGI Examples

2009 Mar 24

URL Encoding GET POST

URL Encoding

The browser, when sending data encode it. Printable characters are sent as themselves, spaces are converted to '+', other characters (e.g. TABs, quotes, ...) are converted to %HH where HH is the hexadecimal value for the ASCII character. The cgi program must do the inverse decoding. The perl CGI.pm module does this for you (for either GET or POST) with:

$value = param('field_name');

GET Method

The information is sent as part of the URL and is available to the CGI program in environment variable QUERY_STRING. The length of the input is specified by environment variable CONTENT_LENGTH. The URL is appended with ? followed by the name and value pairs from each <input> element within the <form> (as: name=value), with each pair separacted by &.


Example G1 - tile clickable image with different color text on it:

In <head> section put following
(change URL as needed):
  <script>
    function pix(pfn) {
      var path, url=document.URL;
      var k;
      // find trailing '/'
      for(k=url.length-1; k>=0; k--) {
        if('/' == url[k]) {
          path = url.substring(0, k);
          break;
        }
      }
      document.write('<td>' + pfn + '<br>' 
         + '<a href=URL/cgi/tilefont.pl?'
         + path + '/' + pfn
         + '><img src=' + pfn + '></a></td>'
      );
    }
  </script>

In <body> section put following
(per tileable image):
  <script>pix('sky001.jpg')</script>

In server cgi directory place following as file "tilefont.pl"
(note path to perl is server dependent):
#!/usr/local/perl588/bin/perl 

print "Content-type: text/html\n\n";

# get query string for path to image to tile
$inp = $ENV{'QUERY_STRING'};
#dbg#print "length($inp)=", length($inp), "<br>\n";

# replace leading "file:///" with "URL"
if(0 == index($inp, "file:///")) {
    substr($inp, 0, 30) = "";
}
#dbg#print "inp=", $inp, "<br>\n";

my $NUM_CSTEP=6;
my @v;
my $r, $g, $b, $color;
for($r=0; $r<$NUM_CSTEP; $r++) {
    @v[$r] = 0x33*$r;
}

print "<html>\n<head>\n  <title>", $inp, " (tiled)</title>\n</head>\n";
print "<body background=", $inp, ">\n\n";

# show different text colors
print "<p align=center><b><font face=\"Courier,Fixed\">\n";
for($r=0; $r<$NUM_CSTEP; $r++) {
    for($g=0; $g<$NUM_CSTEP; $g++) {
        for($b=0; $b<$NUM_CSTEP; $b++) {
            $color = ($v[$r]<<16) + ($v[$g]<<8) + $v[$b];
            printf "<font color=#%06X>#%06X </font>", $color, $color;
        }
        print "<br>\n";
    }
}
print "</font></b></p>\n\n";
# finish page
print "</body>\n</html>\n";

Example G2 - receiving and returning form data:

xxx

POST Method

The information is sent to stdin of the CGI program. The length of the input is specified by environment variable CONTENT_LENGTH. The data consists of name and value pairs from each <input> element within the <form> (as: name=value), with each pair separacted by &.


Example P1 - Receive form data by POST method and reflect it back:

Sample perl code to receive and decode POST data:
#
# receive POST data
read(STDIN, $inp, $ENV('CONTENT_LENGTH');
# decode and store POST data
@pairs = split(/&/, $inp);
%form = ();
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    &value =~ tr/+/ /;
    &value =~ s/%([0-9A-Fa-f]0-9A-Fa-f])/pack("C", hex($1))/eg;
    $form($name) = $value;
}

For a fuller example, here is a full form and the full perl script that reflects it back.


2009