CGI Environment Variables
Environment variables are a series of hidden values that the web server sends to every CGI program you run...
Learn More

Your First Perl Script
All Perl scripts start off with the same line. It tells the server where the Perl interpreter is (Generally this does not matter on a Windows based server)...
Learn More

Partner Directory
Resources 1 2 3 4 5

Business
>> 1 2 3 4 5 6 7 8 9 10 11 12
Computers
>> 1 2 3 4 5 6 7 8 9
Internet
>> 1 2 3 4 5 6 7 8 9 10
Software
>> 1 2 3 4 5
Web Design
>> 1 2 3 4 5
Web Hosting
>> 1 2 3 4 5
Web Promotion
>> 1 2 3 4 5
Web Resources
>> 1 2 3 4 5
Recreation
>> 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Casino
>> 1 2 3 4 5 6
Health
>> 1 2 3 4 5 6 7 8 9 10 11
Shopping
>> 1 2 3 4 5 6 7 8 9 10 11
Miscellaneous
>> 1 2 3 4 5 6 7 8 9 10
Add Your Link

CGI Environment Variables

Environment variables are a series of hidden values that the web server sends to every CGI program you run. Your program can parse them and use the data they send. Environment variables are stored in a hash named %ENV:

Key Value
DOCUMENT_ROOT The root directory 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 your server is running under
QUERY_STRING The query string (see GET, below)
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 (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.cgi101.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; check your server documentation for more information. 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. REMOTE_USER is only set for pages in a directory or subdirectory that's password-protected via a .htaccess file. (See Chapter 20 to learn how to password protect a directory.) 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, short of asking them for it with a web form.

You can print the environment variables the same way you would any hash value:

Let's try printing some environment variables. Start a new file named env.cgi:

Program 3-1: env.cgi - Print Environment Variables Program

    #!/usr/bin/perl -wT
    use strict;
    use CGI qw(:standard);
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    
    print header;
    print start_html("Environment");
    
    foreach my $key (sort(keys(%ENV))) {
        print "$key = $ENV{$key}<br>\n";
    }
    
    print end_html;
    
 
© Copyright 2006 CGIScriptsDir.com All Rights Reserved.