Apache Environment Variables CGI Script

FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Apache Environment Variables CGI Script

Using Apache Environment Variables in .htaccess files with RewriteRule, SetEnvIf, Header, etc., is made easier by having a script that displays current variables and environment. Heres my slightly improved version of the common printenv and test-cgi scripts that come with Apache Installations.

Features of AskApache Environment Debugger

  • Only displays results for user-configured IP address
  • If IP is not allowed it will redirect to any page you specify.
  • Can also be run from the shell
  • Includes both built-in env and also uses perl

Example Output and Variables

printenv and test-cgi script

  • DOCUMENT_ROOT
  • GATEWAY_INTERFACE
  • HTTP_ACCEPT
  • HTTP_ACCEPT_CHARSET
  • HTTP_ACCEPT_ENCODING
  • HTTP_ACCEPT_LANGUAGE
  • HTTP_CACHE_CONTROL
  • HTTP_CONNECTION
  • HTTP_COOKIE
  • HTTP_HOST
  • HTTP_KEEP_ALIVE
  • HTTP_USER_AGENT
  • QUERY_STRING
  • REMOTE_ADDR
  • REMOTE_PORT
  • REQUEST_METHOD
  • REQUEST_URI
  • SCRIPT_FILENAME
  • SCRIPT_NAME
  • SCRIPT_URI
  • SCRIPT_URL
  • SERVER_ADDR
  • SERVER_ADMIN
  • SERVER_NAME
  • SERVER_PORT
  • SERVER_PROTOCOL
  • SERVER_SIGNATURE
  • SERVER_SOFTWARE

The Shell Script

Or Download

#!/bin/sh
###
### ASKAPACHE ENVIRONMENT DEBUGGER - Display Environment Variables using perl and builtin commands
### Version 1.0.2
###
### ---------------------------------------------------------------------------------------
### This work is licensed under the Creative Commons Attribution 3.0 United States License.
### To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/
### or send a letter to Creative Commons, 171 Second Street, Suite 300,
### San Francisco, California, 94105, USA.
### See http://www.askapache.com/about/disclaimer-and-license/ for info.
### ----------------------------------------------------------------------------------------
###
### Usage:
###
###   Place in public web directory request from your web browser.
###
###
### Install:
###
###   1. Place in public web directory like /cgi-bin/ and rename askapache-env.cgi
###   2. chmod 744 or chmod u+x
###   3. Add Options +ExecCGI to the top of /cgi-bin/.htaccess which should be chmod 644
###   4. Add your IP address to the MY_ALLOWED_IP variable in the config below (http://www.askapache.com/online-tools/whoami/)
###
###   Optionally you can add these lines to your .htaccess (http://www.askapache.com/htaccess/htaccess.html)
###   file to only allow from IP Address 208.113.183.103
###
###   Order Deny,Allow
###   Deny from All
###   Allow from 208.113.183.103
 
### CONFIGURATION
MY_ALLOWED_IP="208.113.183.103"
MY_REDIRECT="www.askapache.com"
 
# command line
if [ -e $GATEWAY_INTERFACE ]; then
 
echo "+ ARGS: ${0}"
 
echo "+-----------------------------------------------------+"
echo "| env                                                 |";
echo "+=====================================================+";
env | sort;echo;echo
 
echo "+-----------------------------------------------------+"
echo "| PERL PRINTENV                                       |";
echo "+=====================================================+";
perl -e'foreach $var (sort(keys(%ENV))) {$val = $ENV{$var};$val =~ s|\n|\\n|g;$val =~ s|"|\\"|g;print "${var}=\"${val}\"\n"}';echo;echo
 
echo "+-----------------------------------------------------+"
echo "| declare                                             |";
echo "+=====================================================+";
declare | sort;echo;echo
 
else
 
if [ "$REMOTE_ADDR" == "$MY_ALLOWED_IP" ]; then
 
echo "Content-type: text/plain; charset=iso-8859-1";echo
 
echo "+ ARGS: ${0}"
 
echo "+-----------------------------------------------------+"
echo "| env                                                 |";
echo "+=====================================================+";
env | sort;echo;echo
 
echo "+-----------------------------------------------------+"
echo "| PERL PRINTENV                                       |";
echo "+=====================================================+";
perl -e'foreach $var (sort(keys(%ENV))) {$val = $ENV{$var};$val =~ s|\n|\\n|g;$val =~ s|"|\\"|g;print "${var}=\"${val}\"\n"}';echo;echo
 
#echo "+-----------------------------------------------------+"
#echo "| declare                                             |";
#echo "+=====================================================+";
#declare | sort;echo;echo
 
else
 
echo "Content-type: text/html"
echo "Location: http://${MY_REDIRECT}";
echo "";
 
fi
 
fi
 
exit 0

«
»


My Picks
Someone's Reading
Related Articles
Most Popular 100
Newest Posts
Random

Good Causes

The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. Tim Berners-Lee


Skip to Comments

Add Your Opinion

Reader Comments

  1. Jim Jones ~

    Lets first start I have 2 sites.

    1. a site that uses cgi
    2. a blog – blogger (I understand you can make your blog private but it only allows 100 people to view your site).

    I want to have a cgi script that only allows a list of IP’s I’ve selected to view my site. Than have it so when I put a code like this onto the blog site it will only allow the IP’s I have allowed. In the end of it all, and to sound less confusing. I want to block people on my blogger site, by a CGI script on my paid site.


It's very simple - you read the protocol and write the code. -Bill Joy

HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | TLDP | WAI | DISA | ICSI | GIAC | SANS RR | GHOST | DEFCON | NIST | DHS CYBER | NIST | .:: Phrack Magazine ::.

↑ TOPExcept where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. HTTPD based on NCSA HTTPd

Site Map | Contact Webmaster | Email AskApache | Glossary | License and Disclaimer | Terms of Service