|
Date Published: 1999-12-18
Environment variables are pieces of information gathered by the Common Gateway Interface (CGI). This
data is available to all cgi programs, allowing you to know not only some information specific to your
web server, but also giving you insight on who is visiting your site, with what kind of browser, how
they're accessing your program among other useful information.
All available environment variables are stored in a hash (named
ENV) and can be accessed individually, as with a regular hash
(For a refresher on hashes, please visit our Hashes discussion). So for
example, to figure out what the base www directory on your server is, you would use
$ENV{'DOCUMENT_ROOT'}.
We have listed some the most popular and available environment variables for you below and separated
them into related categories. It is important to note that the available environment variables may
vary from server to server - what's available on one server (running one operating system) can vary
widely from another server running a different OS.
Server Specific Variables:
These variables give you information about the web server that is running your program. These variables are
basically fixed by your server and don't change unless your server or the software your server runs does.
For example, if your web server upgrades it's operating system, that variable
(SERVER_SOFTWARE) can change. The base directory may not.
The left column is the variable name and the right column describes what information the
variable holds and a sample of usage output. Remember, the variable name is accessed by using
$ENV{'VARIABLE_NAME'}.
| DOCUMENT_ROOT |
This is the base directory of a web site.
Example: /www/perlarchive |
| GATEWAY_INTERFACE |
This variable tells you what version of CGI the server complies with.
Example: CGI/1.1 |
| PATH |
This is the directory path that the server looks into to find required programs.
Example: /usr/local/bin:/usr/bin:/bin |
| SERVER_NAME |
This is the web server's IP number or host name.
Example: www.perlarchive.com |
| SERVER_PORT |
This is the port on the server that received the HTTP request.
Example: 80 (80 is standard for most web servers) |
| SERVER_PROTOCOL |
This is the name and version of the protocol being used by the server to process requests.
Example: HTTP/1.1 |
| SERVER_SOFTWARE |
This is the name of the software that the server is running. Often, it will also state the software version number.
Example: Apache/1.3.4 (Unix) FrontPage/4.0.4.3 PHP/3.0.7 |
Request-Specific Variables:
Unlike server specific variables, which are predominantly static, request-specific variables are dynamic.
The request specific variables provide information about what script is being used, what data is being
sent to the script and some information about the user who sent it.
The left column is the variable name and the right column describes what information the
variable holds and a sample of usage output. Remember, the variable name is accessed by using
$ENV{'CONTENT_TYPE'}.
| AUTH-TYPE |
This is the authentication scheme that's used by the server. It uses NULL if no authentication is present.
Example: Basic |
| CONTENT_LENGTH |
This is the number of bytes passed into Standard Input (STDIN) as content from a (form method=) POST request.
Example: 12 |
| CONTENT_TYPE |
This is the type of data being sent to the server.
Example: text/plain |
| PATH_INFO |
This is additional (relative) path information that's passed to the server after the script name, but before any query data.
Example: /programs/bbs |
| PATH_TRANSLATED |
This is the same information as PATH_INFO, but with the
relative path translated to the full server path.
Example: /home/perlarchive/www/cgi-bin/programs/bbs |
| QUERY_STRING |
This is any data that's passed as part of the URL. Data gets included into the url by placing a
? after the script name, and appending the data.
Example: word1=hello&word2=world
A full url can look like:
http://perlarchive.com/test.cgi?word1=hello&word2=world |
| REMOTE_ADDR |
This is the IP number of the user invoking the application.
Example: 206.103.98.48 |
| REMOTE_HOST |
This is REMOTE_ADDR resolved back to the ISP.
Example: ej08-34-121-149-88.ce.mediaone.net |
| REQUEST METHOD |
This is the way the application was accessed. Either by a form "Submit" button that
POSTs to the application, or by a url specially designed to
immediately interact with a program. (See QUERY_STRING above).
Example: GET |
| SCRIPT_NAME |
This is the name of the program that's being run
Example: /perldiver.cgi |
Client Specific Variables
Client-specific variables refer to the web browser that your visitor is using to access your program.
These can be extremely helpful in providing layout/content for specific browsers. Kill those "Click here
if you're using Internet Explorer" and "Click here if you're using Netscape". You have that
information readily available… make your web site seamless.
The left column is the variable name and the right column describes what information the
variable holds and a sample of usage output. Remember, the variable name is accessed by using
$ENV{'USER_AGENT'}.
| HTTP_ACCEPT |
This lists what kind of response schemes are accepted by this request.
Example: */* |
| HTTP_ACCEPT_LANGUAGE |
This is the ISO code for the language the browser is
expecting to receive.
Example:en-us |
| HTTP_AUTHORIZATION |
This identifies authorized users. |
| HTTP_COOKIE |
This is the contents of a cookie (if any) that's set for the domain your web site is located on.
Example: account=name%3A%3Amerceds.com%26id%3A%3A9084179731 |
| HTTP_HOST |
This gives the domain name your web site's located on
Example: perlarchive.com |
| HTTP_USER_AGENT |
This is the browser that the user is surfing with.
Example: Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt) |
You can see your environment variables by either using the variables inside your program, or by
installing a program that prints out this information to you.
Check out the enviroment
variables on The Perl Archive -- Environment variables are the second section.
Jasmine Merced-Ownbey is the President/CEO of
TNS Group, Inc. and the
administrator of The Perl Archive. She also serves as a Director of the
World Organization of Webmasters.
|