Environmental Variables
This document was written by CS 290W TA David Corcoran and was last
modified
When a Web browser sends a CGI request to a Web server, a few important
environmental variables are set which tell you some valuable
information about the browser and the request. Below are a few of
these:
- REQUEST_METHOD
- GET or POST - How the information is being transmitted.
- QUERY_STRING
- The information from the Web form if being transmitted via the GET
method.
- REMOTE_HOST
- The remote machine that is making the request.
- REMOTE_ADDR
- The IP address of the remote machine making the request.
- CONTENT_LENGTH
- The length of the string being passed via POST.
- HTTP_REFERER
- The last page visited by the browser. (Yes, it should be
HTTP_REFERRER! It was mispelled early in Web history. It's
too late to fix it now! Furthermore, HTTP_REFERER cannot be
trusted, for example, if the CGI URL was typed in the location
field.)
- HTTP_USER_AGENT
- The browser the user is using.
A few of these could be quite important depending on what you are
trying to do. For example you could restrict people using the
Internet Explorer browser from executing your CGI script just by
examining the environmental variable HTTP_USER_AGENT. You could also
only allow users from a certain machine or IP address using
REMOTE_HOST or REMOTE_ADDR.
In Perl it is quite easy to get the value of an environmental
variable. Simply use $ENV{'VARIABLE'} with the environmental variable
substituted for VARIABLE that you are interested in. The following
code will illustrate this:
#!/usr/bin/perl
$sBrowser = $ENV{'HTTP_USER_AGENT'};
print "Content-type: text/html \n\n";
if ( $sBrowser =~ "Mozilla" )
{
print "You are using Netscape\n";
}