Working with CGI Variables

Each browser request creates a set of read-only variables that store data about the actual browser and server transactions.

For example, these variables store relevant IP addresses and the browser type that's being used for the client request.

We refer to these variables as CGI environment variables even when the Web server uses an API instead of CGI to communicate with ColdFusion Server.

You can reference CGI environment variables anywhere in a page, for example, to perform conditional processing based on the type of browser that requested the page.

CGI variables

Different browsers and servers support different CGI variables. The table below describes the most common CGI environment variables that are created on the server or created on the client and passed to the server in the request (HTTP) header.

CGI Server Variables Description Server or Client
CGI.SERVER_SOFTWARE The information server software name and version that answers the browser request. Server
SERVER_NAME The server's hostname, DNS alias, or IP address as it appears in self-referencing URLs. Server
GATEWAY_INTERFACE The CGI specification revision to which the server complies. Server
SERVER_PROTOCOL The information protocol name and revision associated with the browser request. Server
SERVER_PORT The port number that received the request. Server
REQUEST_METHOD The HTTP request method. For example, Get or Post. Server
PATH_INFO Extra path information that's supplied by the client. Server
PATH_TRANSLATED A translated version of PATH_INFO supplied by the server. Server
SCRIPT_NAME The virtual path to the script being executed. This is used for self-referencing URLs. Server
QUERY_STRING The query information that follows the question mark (?) in the URL that referenced the script. Server
REMOTE_HOST The requesting machine's hostname when available. Server
REMOTE_ADDR The requesting machine's IP address. Server
AUTH_TYPE The protocol-specific authentication method used to validate users when supported on the server and when the script is protected. Server
REMOTE_USER AUTH_USER The username that the server authenticated when supported on the server and when the script is protected. Server
REMOTE_IDENT The remote username that's retrieved when the HTTP server supports RFC 931 identification. This variable can be used for logging only. Server
CONTENT_TYPE The content type of attached query data. Such as information attached via HTTP POST and PUT, Server
CONTENT_LENGTH The length of the content as described by the client and sent to the server. Server
HTTP_REFERER The referring document that linked or submitted form data to this page. Client
HTTP_USER_AGENT Browser type and revision information for the sending request. Client

Note Note:ColdFusion stores an empty string value for a CGI variable when detailed information is not available for a particular session. For this reason, when performing conditional processing based on a CGI variable, test for null (") rather than a variable's existence.

You will learn about conditional processing in Chapter 8, Programming with ColdFusion.

Referencing CGI variables

Reference CGI environment variables anywhere on a page to use their values. As with any variable that you reference:

Referencing CGI variables example

The code below outputs the current value of the HTTP_USER_AGENT to display the browser type and version:


As you can see, you reference and output CGI variables using the same procedure that you follow when outputting local variables. Move on in this chapter to learn about how you work with and output cookie variables.