07-08-2005
by John Saya
Environment variables are a series of hidden values that the web server
sends to every CGI program that is run. You can use these variables for
many different purposes within your own CGI programs. Environment variables
are stored in a hash named %ENV.
Below is an example of the typical variables that a web server may have
available to you.
Variable | Value |
DOCUMENT_ROOT | The root directory (folder) of your server |
HTTP_COOKIE | The visitor's cookie, if one is set |
HTTP_HOST | The hostname of the page being attempted |
HTTP_REFERER | The URL of the page that called your program |
HTTP_USER_AGENT | The browser type of the visitor |
HTTPS | "on" if the program is being called through a secure server |
PATH | The system path (folder) your server is running under |
QUERY_STRING | The query string passed to your program |
REMOTE_ADDR | The IP address of the visitor |
REMOTE_HOST | The hostname of the visitor (if your server has reverse-name-lookups on; otherwise this is the IP address again) |
REMOTE_PORT | The port the visitor is connected to on the web server |
REMOTE_USER | The visitor's username (for .htaccess protected pages) |
REQUEST_METHOD | GET or POST |
REQUEST_URI | The interpreted pathname of the requested document or CGI (relative to the document root) |
SCRIPT_FILENAME | The full pathname of the current CGI |
SCRIPT_NAME | The interpreted pathname of the current CGI program (relative to the document root) |
SERVER_ADMIN | The email address for your server's webmaster |
SERVER_NAME | Your server's fully qualified domain name (e.g. www.cgiconnection.com) |
SERVER_PORT | The port number your server is listening on |
SERVER_SOFTWARE | The server software you're using (e.g. Apache 1.3) |
Some servers set other environment variables as well. Depending on yours, you may have more or less.
Notice that some environment variables give information about your server, and will never change
(such as SERVER_NAME and SERVER_ADMIN), while others give information about the visitor, and will
be different every time someone accesses the program.
Not all environment variables get set. For example, REMOTE_USER is only set for pages in a directory (folder) or subdirectory
that is password-protected using an .htaccess file. And even then, REMOTE_USER will be the username as it appears
in the .htaccess file; it's not the person's email address. There is no reliable way to get a person's email address
using environment variables.
You can get the value of any variable like this:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "My web browser is: $ENV{'HTTP_USER_AGENT'}<br />\n";
print "My IP Address is: $ENV{'REMOTE_ADDR'}<br />\n";
exit;
When you run the above script, you should see your web browser's name and version, and also your IP Address.
You can see all of the environment variables on your server by running a small Perl script on your web site, shown below.
It's such a simple program, but it gives a lot of valuable information.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
foreach my $key (sort(keys(%ENV))) {
print "$key = $ENV{$key}<br />\n";
}
exit;
Environment variables are used heavily by programmers. They're used to track your IP Address, see what
web site you came from, to transfer information back from your web browser to the CGI script on
your web site, and many other ways.
If you want a quick way to see how your environment variables determine how things run our your web site,
take a look at
System Sleuth.
[ Back ] [ Main Menu ]
Download Fuse Node.js Compiler