A Peek at Environment Variables
May 10, 1999
The term environment variable has a very Unix-ish feel
to it, which is another way of saying that its
meaning is unclear. So let's talk about the weather instead.
At any given time the weather outside contains a number
of environment variables -- in this case, environment
can be taken literally. For instance, if you took
a snapshot of the weather at a precise moment, that
environment would contain several variables: the temperature,
of course, as well as the relative humidity, the barometric
pressure, the wind speed, the wind direction, and so
on.
Similarly, in the event of a CGI process -- that moment when
data is sent from the web browser to the web server
-- there are a number of defined variables which contain
information about the environment at that moment in time.
These variables, created by the web server, are passed into
our Perl program when it is executed via a CGI context
(e.g. over the web).
Take the example of the environment variable named
HTTP_USER_AGENT. This variable reflects the browser
version and platform the web visitor used to access the page.
Thus, the value "Mozilla/4.51 [en] (Win98; I)"
means that the visitor has used Netscape Communicator 4.51
(which identifies itself as "Mozilla"), english
version, on Windows 98. Different browsers identify themselves
differently, so there is not one single reference
for decoding the user agent value. Typically, one reads the
user agent variable to determine or log the browser
or platform being used ... sometimes to redirect the user
to a specific page (which we'll see shortly) or to customize
output messages.
In our example, we could imagine that we'd like to add
the user agent information into the registration log,
for users who register. This would allow us to record the
browser and platform our registrants have used without
explicitly asking them to enter that information. There
are two ways to access a CGI environment variable from
a Perl script, neither of which is inherently superior to
the other:
$ENV{'HTTP_USER_AGENT'}
|
yields |
Mozilla/4.51 [en] (Win98; I) |
$cgiobject->user_agent();
|
yields |
Mozilla/4.51 [en] (Win98; I) |
The %ENV hash in Perl contains keys for each CGI environment
variable. We don't need to be using the CGI module
or object to access the %ENV hash, and so if you have no
other need for the CGI module, you can save some memory
and startup time by simply accessing this hash.
Alternatively, the CGI object possesses methods which access
the same environment variable information. The
method names are similar to but slightly different than
the true environment variable names -- thus, the CGI object's
method user_agent() is used to access the
HTTP_USER_AGENT environment variable.
We can easily modify our registration log code to include
the user agent data in the database:
$useragent=$cgiobject->user_agent();
print LOGFILE
"$username\t$userphone\t$usermail\t$userZIP\t$useragent\n";
The CGI process offers a number of environment variables, but
only those for which a value is known will contain
data. Some environment variables are more "reliable"
than others, meaning that they may or may not contain
useful information. Below is a table summarizing the more
commonly used CGI environment variables, the CGI object
methods you can use to access them, and their reliability.
You can also explore a thoroughly detailed look at
CGI environment variables and associated
CGI Object methods.
Common CGI Environment Variables
|
Environment Variable
$ENV{'Variable'}
|
CGI Object Method |
Description |
Reliability |
| CONTENT_LENGTH |
none |
The number of characters in the query passed to the server via the POST method. |
Good. |
| CONTENT_TYPE |
none |
The MIME type of the query sent via the POST method, such as text/html. |
Good. |
| HTTP_FROM |
user_name |
Supposedly the e-mail address of the user who sent the query; not frequently supported. In the CGI object, a number
of techniques try to glean the user name but this too is unreliable. |
Poor. |
| HTTP_REFERER |
referer |
Typically, the URL of the page from which the CGI query was launched. |
Good. |
| HTTP_USER_AGENT |
user_agent |
The browser and platform used to launch the query. |
Good. |
| QUERY_STRING |
none |
The query string passed to the server via the GET method; the string following the ? in the URL which contains
the keys and values passed to the CGI script. |
Good. |
| REMOTE_ADDR |
remote_host |
The IP address of the machine which sent the query. In the CGI object, this method only returns the IP address
if it cannot determine the hostname. |
Good. |
| REMOTE_HOST |
remote_host |
The hostname (e.g. "dweezil.zappa.com") of the machine which sent the query. In the CGI object, if the
hostname is unknown the IP address is returned. |
Medium. |
| REQUEST_METHOD |
request_method |
Whether the query was sent via GET, POST, or HEAD methods. |
Good. |
Registration Log
The Perl You Need to Know
Detour: Redirection
|