C.7 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

C.7.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. User input is normally assigned to the QUERY_STRING environment variable. In the following example, this variable has no value because the user never was asked for input; that is, the HTML document has no INPUT tags.

The environment variables are set for all requests and are sent by the server to the CGI program. In a Perl program, the environment variables are assigned to the %ENV hash as key/value pairs. They are shown in Table C.8.

Table C.8. CGI environment variables.





Validates user if server supports user authentication



The MIME type of the query data



The number of bytes passed from the server to CGI program



The directory from which the server serves Web documents



The revision of the CGI used by the server



The MIME types accepted by the client

image/gif, image/jpeg, etc


The preferred HTTP connection type



The name of the host machine



The browser (client) sending the request

Mozilla/3.01(X11;I; Sun05.5.1 sun4m)


Extra path information passed to a CGI program



The PATH_INFO translated to its absolute path



The string obtained from a GET request from the URL (information following the ? in the URL)



The remote hostname of the user making a request



The IP address of the host making a request


The port number of the host making a request



The method used to get information to the CGI program

GET, POST , etc


The absolute pathname of the CGI program



The relative pathname of the CGI program; a partial URL



E-mail address of the system administrator



The server's hostname, DNS alias, or IP address



The name and version of the protocol



Name and version of the server software


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. When the browser displays this document, the user can click on the hotlink here and the CGI script, env.cgi , will then be executed by the server. In the HTML document the string here and the URL http://susan/cgi-bin/env.cgi are enclosed in the <A></A> anchor tags. If the hotlink is ignored by the user, the browser displays the rest of the document. The following example is the HTML source file that will be interpreted by the browser. The browser's output is shown in Figure C.5.

Example C.10
 (The HTML file with a hotlink to a CGI script) 1   <HTML> 2   <HEAD> 3   <TITLE>TESTING ENV VARIABLES</TITLE>     </HEAD>     <BODY>     <P>     <H1> Major Test </H1> 4   <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/env.cgi">here</A>  <P>Text continues here...     </BODY>     </HTML> 
Figure C.5. Output of the HTML file in Example C.10. It contains a hotlink to the CGI script.



  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, env.cgi , 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 below, in Example C.11.

Example C.11
 (The CGI 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";     } 


  1. The #! line is important to your server if your server is running on a UNIX platform. It is the path to the Perl interpreter. The line must be the correct pathname to your version of perl or you will receive the following error message from your server:

    Internal Server Error ...

  2. The first line generated by the CGI script is a valid HTTP header, ending with a blank line. The header contains a content type (also called a MIME type) followed by text/plain , meaning that the document will consist of plain text. If the script were to include HTML tags, the content type would be text/html .

  3. The version of the Common Gateway Interface used by this server is printed.

  4. This is a Perl comment line.

  5. The %ENV hash contains environment variables (keys and values) passed into the Perl script from the server. The each function will return both the key and the value and store them in scalars, $ key and $ value , respectively.

  6. The $key/$value pairs are printed back to STDOUT , which has been connected to the server by a pipe mechanism.

Figure C.6. The environment variables displayed by the CGI script in Example C.11.


JavaScript by Example
JavaScript by Example (2nd Edition)
ISBN: 0137054890
EAN: 2147483647
Year: 2003
Pages: 150
Authors: Ellie Quigley

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net