Perl CGI environment variables example

Here's a sample Perl CGI program I wrote that prints out all of the environment variables it knows. I've found this program to be very useful when first installing a web server, or when debugging a problem with a web server or new environment.

Perl CGI environment variables - example program

Here's the code:

#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "<BODY BGCOLOR=black>\n";
print "<FONT COLOR=white><P>";

print "<tt>\n";

foreach $key (sort keys(%ENV)) {
      print "$key = $ENV{$key}<BR>\n";
}

print "</FONT></BODY>"; 

Perl environment variables - example output

And here's some sample output from the program from an Apache web server:

DOCUMENT_ROOT = /home/apache/htdocs
GATEWAY_INTERFACE = CGI/1.1
HTTP_ACCEPT = image/gif, image/x-xbitmap, image/jpeg, image/pjpeg */*
HTTP_ACCEPT_ENCODING = gzip, deflate
HTTP_ACCEPT_LANGUAGE = en-us
HTTP_CONNECTION = Keep-Alive
HTTP_COOKIE = he=llo; in=vue
HTTP_HOST = www.devdaily.com
HTTP_USER_AGENT = Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)
PATH = /bin:/sbin:/usr/bin
QUERY_STRING = 
REMOTE_ADDR = 123.45.67.891
REMOTE_PORT = 2000
REQUEST_METHOD = GET
REQUEST_URI = /cgi-bin/printenv.cgi
SCRIPT_FILENAME = //home/apache/cgi-bin/printenv.cgi
SCRIPT_NAME = /cgi-bin/printenv.cgi
SERVER_ADDR = 123.54.32.1111
SERVER_ADMIN = webmaster@devdaily.com
SERVER_NAME = www.devdaily.com
SERVER_PORT = 80
SERVER_PROTOCOL = HTTP/1.1
SERVER_SIGNATURE = 

Apache/1.1.1 Server at www.devdaily.com Port 80

SERVER_SOFTWARE = Apache/1.1.1 (Unix) mod_jk

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.