Print this Article (NS4)
Netscape Navigator
Internet Explorer
Opera
Neoplanet

Forums
HTML
General
Site Dev
Programming
Flash
Grafix (Art)

Laboratory
Smart HTML
Color Lab
Generators

Contents
Simple CGI
  1. Hello World
  2. Print function
  3. Quoting, EOF
  4. Metacharacters
  5. Special Characters
Perl Basics 1
  1. Variables
  2. Arrays
  3. Hashes
  4. Split function
  5. Subroutines
  6. Defaults
Form CGI
  1. Loops
  2. Conditions
  3. Boolean Statements
  4. Pattern Matching
Time CGI
  1. Local Time
  2. GM Time
  3. time function
Perl Basics 2
  1. Reading Files
  2. Writing Files
  3. Including Files
  4. chop function
  5. chomp function
Guestbook CGI

Redirect CGI

Poll CGI

  1. Giving Commands
  2. Voting
  3. Results Display
  4. Adding Your Vote
Password CGI
  1. Authentification
  2. Multiple Users
  3. Encryption
Mailing List CGI
  1. Sendmail
  2. Multiple Recipients
Unlimited Subdomains CGI

News Grabber CGI

  1. LWP::Simple
Message Board CGI (Part 1)

Back to the Top


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

©1999 Team 26297 "Ad Infinitum Web." All rights reserved. Any reproduction of this document for commercial or redistribution purposes without the permission of the author is forbidden.