Previous | Top | Next | Search | Comments

CGI programming

USING ENVIRONMENT VARIABLES

Getting and using rudimentary input.

Unless your scripts are intened to only display something like the date and time, you will be wanting to get some input from your users in order to create dynamic pages or do some other sort of processing.

As outlined in the Introduction, a proper WWW browser sends requests for data to an HTTP server. These requests also include information about the browser's computing environment. These things might include the Internet name and IP address of the browser's computer, what sort of data the browser can accept, the name of the browser, and the URL the browser is currently displaying. To Perl, this set of information is known as environment variables and can be quite useful in CGI scripting.

EXAMPLE #1

The following example (03-environment.cgi) extracts the data sent by the WWW browser and then creates some simple output based in this data:

"Show me my environment"

As the script demonstrates, quite a lot of useful information is available to CGI scripts through the environment variables. Based on this information alone a person could create simple authentication scripts, or dynamic HTML pages based on the client's operating system or the preferred image format.

EXAMPLE #2

By appending a question mark (?) and some text to the script's URL, an HTML author can supply more input for the exact same script as demonstrated below. (Notice how the value of query string changes from Example #1 and Example #2.)

Rudimentary HTML input

THE CODE

#!/usr/local/bin/perl

# 03-environment.cgi
# This script outputs the environment variables.

# Eric Lease Morgan
# from Becoming a World Wide Web Server Expert
# http://sunsite.berkeley.edu/~emorgan/waves/
# 04/01/97 - renamed file as a .cgi
# 01/20/97 - Martin Luther King Day

# create the http header
$header  = "MIME-Version: 1.0\n";
$header .= "Content-type: text/html\n";
$header .= "\n";

# initalize the html output
$html  = "<html>\n";                   
$html .= "<head>\n";
$html .= "<title>\n";
$html .= "Example #3 - Environment variables\n";
$html .= "</title>\n";
$html .= "</head>\n";
$html .= "<body>\n";

# using brute force, extract each environment variable
$html .= "Environment variables\n";
$html .= "<ol>\n";
$html .= "<li><b>server software</b>:    $ENV{SERVER_SOFTWARE}\n";
$html .= "<li><b>gateway interface</b>:  $ENV{GATEWAY_INTERFACE}\n";
$html .= "<li><b>server protocol</b>:    $ENV{SERVER_PROTOCOL}\n";
$html .= "<li><b>server name</b>:        $ENV{SERVER_NAME}\n";
$html .= "<li><b>server port</b>:        $ENV{SERVER_PORT}\n";
$html .= "<li><b>authorization type</b>: $ENV{AUTH_TYPE}\n";
$html .= "<li><b>remote user</b>:        $ENV{REMOTE_USER}\n";
$html .= "<li><b>remote address</b>:     $ENV{REMOTE_ADDR}\n";
$html .= "<li><b>remote host</b>:        $ENV{REMOTE_HOST}\n";
$html .= "<li><b>remote identity</b>:    $ENV{REMOTE_IDENT}\n";
$html .= "<li><b>request method</b>:     $ENV{REQUEST_METHOD}\n";
$html .= "<li><b>script name</b>:        $ENV{SCRIPT_NAME}\n";
$html .= "<li><b>path</b>:               $ENV{PATH_INFO}\n";
$html .= "<li><b>path translation</b>:   $ENV{PATH_TRANSLATED}\n";
$html .= "<li><b>query string</b>:       $ENV{QUERY_STRING}\n";
$html .= "<li><b>content type</b>:       $ENV{CONTENT_TYPE}\n";
$html .= "<li><b>content length</b>:     $ENV{CONTENT_LENGTH}\n";
$html .= "<li><b>http accept</b>:        $ENV{HTTP_ACCEPT}\n";
$html .= "<li><b>http user agent</b>:    $ENV{HTTP_USER_AGENT}\n";
$html .= "<li><b>http referer</b>:       $ENV{HTTP_REFERER}\n";
$html .= "<li><b>http cookie</b>:        $ENV{HTTP_COOKIE}\n";
$html .= "</ol>\n";

# add a cute line demonstrating the use of one such variable
$html .= "Hello there, $ENV{REMOTE_HOST} It's nice to meet you!\n";

# finish the html
$html .= "</body>\n";
$html .= "</html>\n";

# output the header and html content
print "$header$html";

# exit gracefully
exit;                                    

HOW IT
WORKS

The operation of this script is not much different from the operations of the previous examples:
  1. Like the previous examples, the script begins with a wrapper and some documentation.
  2. It then builds the HTTP header
  3. The creation of the HTML is divided into four parts
    1. Like before, the HTML is initialized with standard codes
    2. Next each value in the array %ENV is extracted and added to the HTML
    3. A simple line is generated including the value of one of the environment variables
    4. The HTML codes is closed
  4. The HTTP header and HTML data returned to the server
  5. The script quits


Previous | Top | Next | Search | Comments

Version: 1.5
Last updated: 2004/12/23. See the release notes.
Author: Eric Lease Morgan (eric_morgan@infomotions.com)
URL: http://infomotions.com/musings/waves/