CGI Environment Variables


When a browser invokes a CGI program, the web server software provides certain environment information to the CGI program. In Perl, this information is available via the %ENV associative array / hash. The exact variables that are available will vary depending on the Web server software being used, but the following is a representative list from Apache.

Environment Variable Description
AUTH_TYPE

The authentication method used to validate a user. Ex.REMOTE_USER or REMOTE_IDENT.

CONTENT_LENGTH

The length of the query data (in bytes or the number of characters)passed to the CGI program through standard input.

CONTENT_TYPE

The media type of the query data, such as "text/html."

DOCUMENT_ROOT

The directory from which Web documents are served.

GATEWAY_INTERFACE

The revision of the Common Gateway Interface that the server uses.

HTTP_ACCEPT

A list of the media types that the client can accept.

HTTP_FROM

The email address of the user making the query (many browsers do not support this variable).

HTTP_REFERER

The URL of the document that the client points to before accessing the CGI program.

HTTP_USER_AGENT

The browser the client is using to issue the request.

PATH_INFO

Extra path information passed to a CGI program.

PATH_TRANSLATED

The translated version of the path given by the variable PATH_INFO.

QUERY_STRING

The query information passed to the program. It is appended to the URL following a question mark (?).

REMOTE_ADDR

The remote IP address from which the user is making the request.

REMOTE_HOST

The remote hostname from which the user is making the request. Only available if the web server performs reverse lookups on client requests. For performance reasons, this is typically not done.

REMOTE_IDENT

The user making the request. Not typically used or available. This variable depends on the use of the "ident" feature of the web server.

REMOTE_USER

The authenticated name of the user making the query. Available if the web server required a user name and password for the page.

REQUEST_METHOD

The method with which the information request was issued (e.g., GET,POST, HEAD).

SCRIPT_NAME

The virtual path (e.g., /cgi-bin/program.pl) of the script being executed.

SERVER_NAME

The server's hostname or IP address.

SERVER_PORT

The port number of the host on which the server is running.

SERVER_PROTOCOL

The name and revision of the information protocol the request came in with.

SERVER_SOFTWARE

The name and version of the server software that is answering the client request.

The variables REQUEST_METHOD, CONTENT_LENGTH, and QUERY_STRING are important for processing user input. The variable HTTP_USER_AGENT can be important if a program is customizing it's output for specific browsers.

The following program will dump out the environment variables passed to a CGI program.


Next Previous Examples

Copyright 2001 - Andy Welter