Using environment variables

This script demonstrates the usage of CGI environment variables.

Programming Issues

This script uses a template HTML file for its output. Rather than using print to output the body of our document, we assign it to the variable $mainmatter and pass that to the printHTMLOutputFromTemplate subroutine at the end of the script.

Firstly, we loop through all the environment variables using Perl's foreach operator to accomplish two tasks:

Secondly, we present an HTML table showing some of the more commonly available environment variables. We also give an example of explicitly determining the user's browser along with its major and minor version numbers, where available, and the user's operating system from the HTTP_USER_AGENT environmental variable. This is a common technique for implementing conditional HTML, where different code is servers to different user agents depending on their capabilities. The Browser Capabilities Project maintains a list of known user agents and their identifiers along with a database of their capabilities for this purpose, which is far more comprehensive than the current example which is merely a demonstration.

Thirdly, we present a few paragraphs showing how some of the environment variables might be used to identify the user agent, the remote IP address, and so on.

Finally, we again use Perl's foreach operator to output all the environment variables implemented by the server and their contents. In the source code this section is commented out, and it is not implemented on the CGI script used on this site, as it is generally considered to be a bad idea to reveal the contents of all the server's environment variables to anybody who cares to look at them, since they may reveal information (such as absolute paths and server software) that may be useful to an attacker. If you want to view these results on your own web server, then simply uncomment that section, but do so at your own risk.

Note that all the environment variables is not quite correct. Many times some environment variables may only be defined in certain circumstances. For instance, the CONTENT_LENGTH environment variable may only be defined when the REQUEST_METHOD is POST. Similarly, HTTP_REFERER may not be defined if the script was called directly, rather than from a hyperlink on another page.

A list of standard CGI environmental variables is available.

Usage

Just run the CGI script by requesting it with the link: http://www.paulgriffiths.net/cgi-bin/envvars.pl.

Source and Downloads