CGI Environmental Variables

Much of the information needed by CGI programs is made available via Environment Variables. Environment Variables are used to pass data about the CGI request from the server to the script. The representation of the characters in the Environment Variables is system defined. They are accessed by the script in a system dependent manner. Programs can access this information as they would any Environment Variable (e.g., via the %ENV associative array in Perl). A missing Environment Variable is equivalent to a zero-length (NULL) value, and vice versa.

The table below lists the Environment Variables commonly available through CGI. Here they are shown using a canonical representation of capitals plus underscore ("_"). The actual representation of the names is system defined; for a particular system the representation may be defined differently to this. Check with your own server documentation for more information.

ENVIRONMENT VARIABLES

VARIABLE DESCRIPTION
AUTH_TYPE The authentication method the server uses when a client requests a protected script
CONTENT_LENGTH The length of the content as sent by the client
CONTENT_TYPE Type of content that was sent to the server. Returned as MIME types
DATE_GMT The current system date in Greenwich Mean Time
DATE_LOCAL The current system date in the local time zone
GATEWAY_INTERFACE The current CGI revision level that is supported by the host server
HTTP_[header name] All of the HTTP header information that will appear in a comma separated list
HTTP_ACCEPT The MIME types that the browser can accept
HTTP_ACCEPT_AGENT The languages that the browser can accept
HTTP_REFERER The URL of the page that referred the client to the document on your site
LAST_MODIFIED The date the document was last modified
LOGON_USER The account the user is logged into. Only set when accessing protected scripts
PATH_INFO Additional information about the document path, returned with a virtual path name
PATH_TRANSLATED PATH_INFO with the virtual path mapped to the directory path
QUERY_STRING The information which follows the ? in the URL which referenced this script. It should not be decoded in any fashion.
REMOTE_ADDR Client's IP Address
REMOTE_HOST Name of the host corresponding to REMOTE_ADDR. If this isn't available to the server, it is left empty
REMOTE_IDENT If the HTTP server supports RFC 931 identification, then this variable will be set to the remote user name retrieved from the server.
REMOTE_USER If the server supports user authentication, and the script is protected, this is the username they have authenticated as.
REQUEST_METHOD Method the request was made with. Most common methods are GET and POST.
SCRIPT_NAME Virtual path to the script being executed. Use it to create self-referencing forms or for creating "Go back" links for scripts that can be accessed from different pages
SERVER_NAME Server name as used in the URL. Can be a hostname, IP Address, or DNS alias.
SERVER_PORT Port number which the request was sent
SERVER_PORT_SECURE Values of 0 or 1, depending on whether this request was made on a secured port (1) or not (0)
SERVRE_PROTOCOL The name and revision of the information protocol this request came in with.
SERVER_SOFTWARE The name and version of the information server software answering the request (and running the gateway). Format: name/version. Server software, for example "Microsoft-IIS/3.0"
URL Base portion of the URL


Sample Script (Script in Action)

Here's a simple Perl CGI script that uses Environment Variables to display various information about the server:

#!/usr/local/bin/perl
print "Content-type: text/html", "\n\n";
print "<HTML>", "\n";
print "<HEAD><TITLE>About this Server</TITLE></HEAD>", "\n";
print "<BODY><H1>About this Server</H1>", "\n";
print "<HR><PRE>";
print "Server Name:       ", $ENV{'SERVER_NAME'}, "<BR>", "\n";
print "Running on Port:   ", $ENV{'SERVER_PORT'}, "<BR>", "\n";
print "Server Software:   ", $ENV{'SERVER_SOFTWARE'}, "<BR>", "\n";
print "Server Protocol:   ", $ENV{'SERVER_PROTOCOL'}, "<BR>", "\n";
print "CGI Revision:      ", $ENV{'GATEWAY_INTERFACE'}, "<BR>", "\n";
print "<HR></PRE>", "\n";
print "</BODY></HTML>", "\n";
exit (0);

The preceding program outputs the contents of five Environment Variables into an HTML document. In Perl, you can access the Environment Variables using the %ENV associative array. Here's a typical output of the program:

Sample Script Output

<HTML>
<HEAD><TITLE>About this Server</TITLE></HEAD>
<BODY>
<H1>About this Server</H1>
<HR><PRE>
Server Name:          bermuda
Running on Port:      80
Server Software:      Microsoft-IIS/4.0
Server Protocol:      HTTP/1.1
CGI Revision:         CGI/1.1
</PRE><HR>
</BODY>
</HTML>
Internet Explorer Additional Headers

Internet Explorer sends additional headers to your server that aren't supported by Netscape Browsers. These additional headers deal with the capabilities of each client' computer.

HEADER

DESCRIPTION

HTTP_UA_COLOR The color palate of the browser display
HTTP_UA_CPU Returns the CPU type of the client's computer, for example, "x86" for all Intel compatible computers. You can use this information to decide which ActiveX control packages to send to this computer (Intel, Mac, Alpha, or Mips version).
HTTP_UA_OS The operating system of the client browser
HTTP_UA_PIXELS The resolution of the client browser display

The PATH_TRANSLATED

When a Web Browser makes an HTTP request to a Web Server, the URL of the request is stored with all the other server variables. The server then translates this URL, which is a virtual web path, to a physical file path containing the actual location of the file on the Web Server. This value is stored in the Server Variables collection as PATH_TRANSLATED.

For example:

URL Request http://MyServer/Files/ShowDir.asp
PATH_INFO /Files/ShowDir.asp
PATH_TRANSLATED C:\inetpub\wwwroot\testserver\files\ShowDir.asp

So we need to retrieve this information from the Web Server, and write it to the response. To be able to access the server variables, we first need to be able to reference the Request object. Once we have determined the value of PATH_TRANSLATED, we can output that information to the response. In order to do this, we need to be also able to reference the Response object.