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.

Name

Value

Example

AUTH_TYPE

Validates user if server supports user authentication

 

CONTENT_TYPE

The MIME type of the query data

text/html

CONTENT_LENGTH

The number of bytes passed from the server to CGI program

Content-Length=55

DOCUMENT ROOT

The directory from which the server serves Web documents

/opt/apache/htdocs/index.html

GATEWAY_INTERFACE

The revision of the CGI used by the server

CGI/1.1

HTTP_ACCEPT

The MIME types accepted by the client

image/gif, image/jpeg, etc

HTTP_CONNECTION

The preferred HTTP connection type

Keep-Alive

HTTP_HOST

The name of the host machine

susan

HTTP_USER_AGENT

The browser (client) sending the request

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

PATH_INFO

Extra path information passed to a CGI program

 

PATH_TRANSLATED

The PATH_INFO translated to its absolute path

 

QUERY_STRING

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

http://susan/cgi-bin/form1.cgi?Name=Christian+Dobbins

REMOTE_HOST

The remote hostname of the user making a request

eqrc.ai.mit.edu

REMOTE_ADDR

The IP address of the host making a request

192.100.1.11

REMOTE_PORT

The port number of the host making a request

33015

REQUEST_METHOD

The method used to get information to the CGI program

GET, POST , etc

SCRIPT_FILENAME

The absolute pathname of the CGI program

/opt/apache/cgi-bin/hello.cgi

SCRIPT_NAME

The relative pathname of the CGI program; a partial URL

/cgi-bin/hello.cgi

SERVER_ADMIN

E-mail address of the system administrator

root@susan

SERVER_NAME

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

susan, 127.0.0.0

SERVER_PROTOCOL

The name and version of the protocol

HTTP/1.0

SERVER_SOFTWARE

Name and version of the server software

Apache/1.2b8

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.

graphics/ap03fig05.jpg

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, 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";     } 

EXPLANATION

  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.

graphics/ap03fig06.jpg



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

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