
Perl Case Study - Environment CGI
By Lisa Hui
Environment variables are akin to persistent global variables that exist
without you having to explicitly declare them in the script or program and
are useful because they contain data about the server and system that the
script is running on, the path, and even a little bit about the visitor -
the IP address. This is data that "resides in the environment" basically.
Below is a script that prints out a list of environment variables. They are
called simply as another variable in a Perl script:
$ENV{'NameOfVariable'}. It has the typical dollar sign and
a set of encompassing curly braces {} which we might equate with a hash and
its key (NameOfVariable).
#!/usr/local/bin/perl
#################
# File Name: environment.cgi
# This file demonstrates the use of server environment variables
# which are globally accessible by any CGI script
print qq~
<HTML>
<Head><Title>AIW - Environment Variable
Test</Title></Head>
<Body>
<TABLE>
<TR><TD>\$ENV{'AUTH_TYPE'}</TD><TH>$ENV{'AUTH_TYPE'}</TH></TR>
<TR><TD>\$ENV{'CONTENT_LENGTH'}</TD><TH>$ENV{'CONTENT_LENGTH'}</TH></TR>
<TR><TD>\$ENV{'CONTENT_TYPE'}</TD><TH>$ENV{'CONTENT_TYPE'}</TH></TR>
<TR><TD>\$ENV{'GATEWAY_INTERFACE'}</TD><TH>$ENV{'GATEWAY_INTERFACE'}</TH></TR>
<TR><TD>\$ENV{'HTTP_REFERER'}</TD><TH>$ENV{'HTTP_REFERER'}</TH></TR>
<TR><TD>\$ENV{'PATH'}</TD><TH>$ENV{'PATH'}</TH></TR>
<TR><TD>\$ENV{'PATH_INFO'}</TD><TH>$ENV{'PATH_INFO'}</TH></TR>
<TR><TD>\$ENV{'PATH_TRANSLATED'}</TD><TH>$ENV{'PATH_TRANSLATED'}</TH></TR>
<TR><TD>\$ENV{'QUERY_STRING'}</TD><TH>$ENV{'QUERY_STRING'}</TH></TR>
<TR><TD>\$ENV{'REMOTE_ADDR'}</TD><TH>$ENV{'REMOTE_ADDR'}</TH></TR>
<TR><TD>\$ENV{'REMOTE_HOST'}</TD><TH>$ENV{'REMOTE_HOST'}</TH></TR>
<TR><TD>\$ENV{'REMOTE_IDENT'}</TD><TH>$ENV{'REMOTE_IDENT'}</TH></TR>
<TR><TD>\$ENV{'REMOTE_USER'}</TD><TH>$ENV{'REMOTE_USER'}</TH></TR>
<TR><TD>\$ENV{'REQUEST_METHOD'}</TD><TH>$ENV{'REQUEST_METHOD'}</TH></TR>
<TR><TD>\$ENV{'SCRIPT_FILENAME'}</TD><TH>$ENV{'SCRIPT_FILENAME'}</TH></TR>
<TR><TD>\$ENV{'SCRIPT_NAME'}</TD><TH>$ENV{'SCRIPT_NAME'}</TH></TR>
<TR><TD>\$ENV{'SERVER_ADMIN'}</TD><TH>$ENV{'SERVER_ADMIN'}</TH></TR>
<TR><TD>\$ENV{'SERVER_NAME'}</TD><TH>$ENV{'SERVER_NAME'}</TH></TR>
<TR><TD>\$ENV{'SERVER_PORT'}</TD><TH>$ENV{'SERVER_PORT'}</TH></TR>
<TR><TD>\$ENV{'SERVER_PROTOCOL'}</TD><TH>$ENV{'SERVER_PROTOCOL'}</TH></TR>
<TR><TD>\$ENV{'SERVER_SOFTWARE'}</TD><TH>$ENV{'SERVER_SOFTWARE'}</TH></TR>
</TABLE>
<P>
</Body>
</HTML>
~;
See this script in action:
environment.cgi
You can easily use this to configure what domain names can access this script
(or even exact IP address). In particular, the query string, and request
method are useful in passing data (the former isn't a very secure method
- but suits well for publicly accessible type scripts).
If you haven't seen it yet, I suggest you see an application of the
REQUEST_METHOD environment variable used in the Form
CGI Case Study.

Perl Case Study - Form CGI