69 Seeing the CGI Environment


#69 Seeing the CGI Environment

Sometimes scripts can be quite simple and still have useful results. For example, while I was developing some of the scripts for this chapter, Apple released its Safari web browser. My immediate question was, "How does Safari identify itself within the HTTP_USER_AGENT string?"

Finding the answer is quite a simple task for a CGI script, a script that can be written in the shell.

The Code

 #!/bin/sh # showCGIenv - Displays the CGI runtime environment, as given to any #    CGI script on this system. echo "Content-type: text/html" echo "" # Now the real information echo "<html><body bgcolor=\"white\"><h2>CGI Runtime Environment</h2>" echo "<pre>" env  printenv echo "</pre>" echo "<h3>Input stream is:</h3>" echo "<pre>" cat - echo "(end of input stream)</pre></body></html>" exit 0 

How It Works

When a query comes from a web client to a web server, the query sequence includes a number of environment variables that the web server (Apache, in this instance) hands to the script or program specified (the so-called Common Gateway Interface). This script displays this data by using the shell env command, with the rest of the script being necessary wrapper information to have the results fed back through the web server to the remote browser.

Running the Script

To run this code, you need to have the script executable and located on your web server. (See the earlier section "Running the Scripts in This Chapter" for more details.) Then simply request the saved .cgi file within a web browser.

The Results

click to expand
Figure 8-1: The CGI runtime environment, from a shell script



Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net