[thelist] CGI VARIABLES

Anthony Baratta Anthony at Baratta.com
Fri, 07 Jan 2000 15:23:39 -0800


Adrian Fischer wrote:
> 
> Greetings all,
> 
> Off the top of your head is there a way I can get a print of all the variables being used
> withing a cgi script i.e. what is assigned to them at a given time?

You need to be a bit more specific. If you are talking about variables internal to
your CGI script, then you know your program best and should create your own
sub-routine to dump values.

If you want to see what the form sent to your script and all the Environment
variables the web server gives your script, you can use the following code:

Here's a quick and dirty perl script that will output all the variables given to 
it as well as the environment variables Apache is making available.

Its uses cgi-lib.pl - which you can get here:

        http://cgi-lib.stanford.edu/cgi-lib/

---- Cut here ------

#!/usr/local/bin/perl

  require "cgi-lib.pl";

## Start off by returing the text-type to the browser
## This will help in preventing a browser timeout
  print &PrintHeader;

## Print HTML Page Header
  print "<HTML><BODY> \n";
  
## Parsing Form Data for cgi analysis
  $ret=&ReadParse(*input);

## Printing out variables received....
  print "<UL><h1>Variables submitted via Form</h1>  \n";
  foreach $key (sort keys %input) {
    print "<b>$key</b> = <i>$input{$key}</i> <br>\n";
  } # end   
  
## Printing out variables received....
  print "<h1>Variables from Environment</h1> \n";
  foreach $key (sort keys %ENV) {
    print "<b>$key</b> = <i>$ENV{$key}</i> <br>\n";
  } # end   
    
## Print HTML Page Footer
  print "</ul></BODY></HTML> \n";

  exit;

---- Cut here ------

an Example of the output.....

     Variables submitted via Form

     ComboBox = Option3
     TextBox = Very long message
     RadioButton = Choice1
     Submit = Submit

     Variables from Environment

     GATEWAY_INTERFACE = CGI/1.1 
     HTTP_ACCEPT = image/gif, image/x-xbitmap, image/jpeg, image/pjpeg
     HTTP_ACCEPT_CHARSET = iso-8859-1,*,utf-8 
     HTTP_ACCEPT_ENCODING = gzip 
     HTTP_ACCEPT_LANGUAGE = en 
     HTTP_HOST = www.foo.com 
     HTTP_REFERER = http://www.foo.com/pagethatcontainslinktocgi.html
     HTTP_USER_AGENT = Mozilla/4.61 [en] (Win95; U) 
     PATH = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin 
     QUERY_STRING = 
     REMOTE_ADDR = 259.342.548.278 
     REMOTE_HOST = 259-342-548-278.modem.dialin.net 
     REQUEST_METHOD = GET 
     SCRIPT_NAME = /path/to/env.cgi 
     SERVER_NAME = www.foo.com 
     SERVER_PORT = 80 
     SERVER_PROTOCOL = HTTP/1.0 
     SERVER_SOFTWARE = BESTWWWD/2.4 

---- Cut here ------

You can emulate a link click by placing this link on your page to see HTTP_REFERER
output:

    <A HREF="http://www.foo.com/cgi-bin/env.cgi">Click Here for output</a>

If you call the script directly there is no HTTP_REFERER. 

Also you can POST to this script to verify that your forms are giving the proper
information to your cgi script. (I've used this script with FLASH 4.0 forms to verify
their 'POST' output.)

-- 
Anthony Baratta
President
KeyBoard Jockeys
                    South Park Speaks Version 3 is here!!!
                       http://www.baratta.com/southpark
                              Powered by Tsunami