Mastering the Web
Contents
Website Planning Tutorial
Website Design Tutorial
HTML Tutorial
HTML Tables Tutorial
CGI Tutorial
JavaScript Tutorial
Perl Tutorials
CSS Tutorial
Installing a Web Server
Security Tutorial
HTML Cookies Tutorial
Web Tracking Tutorial
Download Free Programs
F.A.Q.

  1. Introduction to CGI
  2. CGI Environment Variables
  3. Using CGI Scripts
  4. Developing a CGI Script
  5. A Form-to-mail CGI Script

Download FWTLogstat1

Download FWTLogstat2

Developing a CGI Script

A key aspect to develop a CGI script is to master how data are exchanged between the HTTP client (browser, navigator), the Web server (Apache, etc.), and the script. When the GET method is used, data are sent after a question mark following the URL of the script. Fields are delimited with ampersand signs; spaces, the plus sign, and other special characters are coded as the ordinal number of the character in the ASCII collating sequence, using the hexadecimal numbering system, and preceded by a percent sign. For instance, a space ends being the three-character sequence: %20. This encoding method is known as URL-encoding.

Data sent to a CGI script using URL-encoding is known as a "query string". A query string usually consists in several pairs of keys and values, the keys being the names of the fields and the values the data input by the user. The following Perl script decodes such a query string.

    # split the query string into an array of keywords
    foreach $field (split("&", $ENV{QUERY_STRING})) {
      # get the keyword and value pair from the field string
      if ($field =~ /(.*)=(.*)/)  {
         ($key, $value) = ($1, $2); 
          $value =~ s/\+/ /g ; # replace "+" with " "
          # unescape ASCII hexadecimal characters
          $value =~ s/%(..)/pack('c',hex($1))/eg;
          $inputs{$key} = $value; # add keyword/value pair to a list
      }
    }

The POST method employs another strategy to hand data to the script. Every program in the Unix environment has files implicitly defined and opened. They are called the standard input, the standard output, and the standard error. When using the POST method, user data is channelled through the standard input of the script. In reality, POST has a wider utility than sending form data to a script. As its name implies, its goal is to create an HTTP object at the server. The new object is returned to the client.

For instance, if you have access to the server using the "telnet" program, you can make a POST request. If the URL of the server is "www.yourdomain.com" and it listens at port 80, you can type the following command.

>telnet www.yourdomain.com 80

When you get connected, type the following.

>POST /public_html/cgi-bin/env.pl HTTP/1.0
>

Note the blank line after the command because it is obligatory. As a result, the server will send you these headers.

HTTP/1.0 200 0K
Date: Monday, 23-Jun-08 21:11:13 GMT
Server: Apache 2.0
MIME-version: 1.0
Content-type: text/html

After the headers, you will see the HTML code of the page already shown that displays the environment variables' values.

When POST places data in the script's standard input, it also places the number of bytes in the variable CONTENT_LENGTH. Therefore, the following piece of code can be utilized to get the data.

    # How many bytes are we supposed to receive?
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    # make a list of keyword/value pairs
    @pairs = split(/&/, $buffer);
    # cycle through each pair and decipher the values
    foreach $pair (@pairs)
    {
        # get the name/value pair strings
        ($name, $value) = split(/=/, $pair);
        # translate "+" to a space
        $value =~ tr/+/ /;
        # decipher ASCI hexidecimal escaped characters, if any
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
        # add the pair to a list keyed on the name of the variable
        $contents{$name} = $value;
    }

Previous | Contents | Next

| HOME | FEEDBACK | BOOKMARK |
Build your Website
© 1999-2008 Hector Castro -- All rights reserved

If your doubt is not answered in this site, please use the
contact form .
I'll answer as soon as posible.
I can help you using instant messaging. To schedule a meeting, please use the
meeting form.
You will find the late news about the free programs offered here on my blog
Free Webmaster Tools
You can get news about updates to my free programs through this
RSS feed.

www.great-web-info.com