Previous Page Next Page

16.5. Getting Information Into and Out of the CGI Script

The server and the CGI script communicate in four major ways. Once the browser has sent a request to the server, the server can then send it on to the CGI script. The CGI script gets its input from the server as:

  1. Environment variables

  2. Query strings

  3. Standard input

  4. Extra path information

After the CGI program gets the input from the server, it parses and processes it and then formats it so that the server can relay the information back to the browser. The CGI script sends output through the gateway by

  1. Generating new documents on the fly

  2. Sending existing static files to the standard output

  3. Using URLs that redirect the browser to go somewhere else for a document

16.5.1. CGI Environment Variables

The CGI program is passed a number of environment variables from the server. The environment variables are set when the server executes the gateway program and are set for all requests. The environment variables contain information about the server, the CGI program, the ports and protocols, path information, etc., and they are always represented in uppercase. User input is normally assigned to the QUERY_STRING environment variable. In the following example, this variable has no value, because the user was never asked for input; that is, the HTML document has no INPUT tags.

Example 16.9.

(The CGI Perl Script)

1   #!/bin/perl
2   print "Content type: text/plain\n\n";
3   print "CGI/1.1 test script report:\n\n";
4   # Print out all the environment variables
5   while(($key, $value)=each(%ENV)){
6       print "$key = $value\n";
    }

Figure 16.8. The QUERY_STRING environment variable.


Have you ever noticed, when performing a search in your browser, you end up with a line that looks like this?

http://www.gogle.com/search?hl=en&q=related%3Awww.lynda.com%2Fhexh.html&btnG=Search

					  

The information after the ? is sent by the browser to the server and assigned to the QUERY_STRING environment variable. It is a URL encoded by the browser; i.e., any nonalphanumeric characters in the string are represented by +, &, or a hexadecimal number preceded by a %, such a %2. The program on the server side will be responsible for decoding this string to make it legible. For our discussion, that program is a Perl CGI script, although it could be a C, C++, Tcl, Shell script, etc.

In the next example, we have appended a ? and string of text to the URL of the same CGI script we executed in Example 16.9. This string is encoded by the browser and assigned to the QUERY_STRING environment variable, making the information available to the Perl script. The next step would be to remove the %20 (hexadecimal value of a space); i.e., to decode the encoded string. All of this will be explained in detail on page 545, but for now, when you see this type of URL, the string after the ? is sent to your program in the QUERY_STRING environment variable.

Table 16.6. CGI Environment Variables (Must Be Uppercase)
NameValueExample
AUTH_TYPEValidates user if server supports user authentication 
CONTENT_LENGTHThe number of bytes passed from the server to CGI programContent-Length=55
CONTENT_TYPEThe MIME type of the query datatext/html
DOCUMENT ROOTThe directory from which the server serves Web documents/opt/apache/htdocs/index.html
GATEWAY_INTERFACEThe revision of the CGI used by the serverCGI/1.1
HTTP_ACCEPTThe MIME types accepted by the clientimage/gif, image/jpeg, etc.
HTTP_CONNECTIONThe preferred HTTP connection typeKeep-Alive


A question mark has been appended to the URL, followed by a string of text.

An HTML File with a Link to a CGI Script

The following example is an HTML file that will allow the user to print out all the environment variables by clicking on a hyper-link. When the browser displays this document, the user can click on the link here, and the CGI script printenv.pl will then be executed by the server. In the HTML document, the string here and the URL http://localhost/cgi-bin/printenv.pl are enclosed in the <A href></A> tags. If the link is ignored by the user, the browser displays the rest of the document. The following example is the HTML source file that will be displayed in the browser. The browser's output is shown in Figure 16.9.

Figure 16.9. Now the QUERY_STRING environment variable has a value.


Example 16.10.

(The HTML file with a hotlink to a CGI script)
1   <html>
2   <head>
3   <title>TESTING ENV VARIABLES</title>
    </head>
    <body>
4   <p>
    <h1> MAJOR TEST </h1>
    <p> if you would like to see the environment variables<br>
    being passed on by the server, click .
5   <a href="http://localhost/cgi-bin/printenv.pl">here</a>
    <p>text continues here...
    </body>
    </html>

Explanation

  1. The <HTML> tag says this document is using the HTML protocol.

  2. The <HEAD> tag contains the title and any information that will be displayed outside the actual document.

  3. The <TITLE> tag is displayed in the top bar of the browser window.

  4. The <P> tag is the start of a paragraph. The <BR> tag causes the line to break.

  5. The <A> tag is assigned the path to the CGI script printenv.pl on server localhost. The word here will be displayed by the browser in blue underlined letters. If the user clicks on this word, the CGI script will be executed by the server. The script will print out all of the environment variables passed to the script from the server. This is one of the ways information is given to a CGI script by a Web server. The actual CGI script is shown in Example 16.9.

Previous Page Next Page