Print all CGI environment vairables

Discussion in 'Perl' started by pradeep, Nov 27, 2006.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    At some point while writing a CGI script in Perl, many newbies have needed to print all the environment varibles in Perl, sometimes for debugging or for the sake of curiosity.
    Well, here is the code to print all the CGI environment variables.

    Code:
    #!/usr/bin/perl
     
     print "Content-type: text/html\n\n";
     print "<pre>\n";
     foreach $key (sort keys(%ENV)) 
     {
       print "$key = $ENV{$key}<p>";
     } 
    Output:
    Code:
    DOCUMENT_ROOT = /share/htmlGATEWAY_INTERFACE = CGI/1.1
     
     HTTP_ACCEPT = text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
     
     HTTP_ACCEPT_CHARSET = ISO-8859-1,utf-8;q=0.7,*;q=0.7
     
     HTTP_ACCEPT_ENCODING = gzip,deflate
     
     HTTP_ACCEPT_LANGUAGE = en-us,en;q=0.5
     
     HTTP_CACHE_CONTROL = max-age=0
     
     HTTP_CONNECTION = keep-alive
     
     HTTP_HOST = 127.0.0.1
     
     HTTP_KEEP_ALIVE = 300
     
     HTTP_USER_AGENT = Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0
     
     PATH = /sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin
     
     QUERY_STRING = 
     
     REMOTE_ADDR = 127.0.0.1
     
     REMOTE_PORT = 4520
     
     REQUEST_METHOD = GET
     
     REQUEST_URI = /cgi-bin/test.pl
     
     SCRIPT_FILENAME = /share/cgi-bin/test.pl
     
     SCRIPT_NAME = /cgi-bin/test.pl
     
     SERVER_ADDR = 127.0.0.1
     
     SERVER_ADMIN = root@localhost
     
     SERVER_NAME = localhost
     
     SERVER_PORT = 80
     
     SERVER_PROTOCOL = HTTP/1.1
     
     SERVER_SIGNATURE = 
     
     Apache/2.0.52 (Red Hat) Server at localhost Port 80
     SERVER_SOFTWARE = Apache/2.0.52 (Red Hat)
     
     
     
  2. oleber

    oleber New Member

    Joined:
    Apr 23, 2007
    Messages:
    37
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software Developer (Perl, C/C++ and Java)
    Location:
    Hamburg, Germany
    Home Page:
    http://oleber.freehostia.com/
    This may be a good point to introduce the module Data::Dumper.

    This module is one of the most useful modules for this kind of debug.

    Code:
    #!/usr/bin/perl
    use Data::Dumper;
     
    print "Content-type: text/html\n\n";
    print "<html><head/><body><pre>\n";
    
    print Dumper(\%ENV);
    
    print "</pre></body></html>\n";
    
    Note: I'm not working in the internet environment so some errors can appear on my code, but the idea is there ;)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice