CGI Query Acception and Form Submission


CGI Environment Variables

Though some CGI programs accept no input data, most CGI applications do need to interactively communicate with the user - they need to receive information (or a query) from the users. This is why CGI is so important. CGI programs receive this information from CGI Environment Variables.

Usually, a CGI program receives two types of the information from the browser:

  1. Information about the browser (its type, what it can view, the remote host name, and so on), the server (its name and version, the port it's running on, and so on), and the CGI program itself (the program name and where it's located). The server provides all of this information to the CGI program through environment variables.
  2. Information entered by the user. This information, after first being encoded by the browser, is sent either through an environment variable (the GET method) or through the standard input (stdin- the POST method).
Click this button to get all the CGI environment variables for GET method:
and this button for POST method:

Here is the description of some of the important CGI environment variables you just read:

Encoding Scheme

Form data consists of a list of name/value pairs. Before transmitting this data to the server and the CGI program, the browser encodes the information using a scheme called URL encoding (specified by the MIME type application/x-www-form-urlencode). The encoding scheme consists of the following: For example, suppose you have the following name/value pairs:
     name    Zhanshou Yu
     major   Computer Science  
     e-mail  zyu@bayou.uh.edu
In order to encode these pairs, first replace the non-alphanumeric characters. In this example, only one charater exists,@, which is replaced with %40. So we have:
     name    Zhanshou Yu
     major   Computer Science  
     e-mail  zyu%40bayou.uh.edu
Now, replace all spaces with a plus sign. We get:
     name    Zhanshou+Yu
     major   Computer+Science  
     e-mail  zyu%40bayou.uh.edu
Seperate each name and value with an equal sign:
     name=Zhanshou+Yu
     major=Computer+Science  
     e-mail=zyu%40bayou.uh.edu
Finally, separate each pair with an ampersand (&):
name=Zhanshou+Yu&major=Computer+Science&email=zyu%40bayou.uh.edu
The CONTENT_LENGTH is equal to the number of character in the coding string. This example has 64 characters, so the CONTENT_LENGTH is 64.

Submission Method

As we mentioned before, there are two methods that we can use to submit a form: GET method and POST method. Just look at the above two bold input lines (it doesn't matter if you do not understand other statements right now). The POST method using:
 read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
to read the input data from standard input to a buffer. The length is specified by the enviroment variable CONTENT_LENGTH. If you are a C language programmer, the following C code can do the same work :
 char *buffer;
 char *contentLength=getenv("CONTENT_LENGTH");
 int length=atoi(contentLength);
 buffer=(char *)malloc(length+1);
 fread(buffer,1,length,stdin);
While for the GET method, the input data is packeted into the QUERY_STRING environment variable so we just assign it to a buffer. PERL code is :
 $buffer=$ENV{'QUERY_STRING'};
or you can write in C code:
 char *buffer=getenv("QUERY_STRING");

Previous PageTable of ContentNext Page