|
As you become more advanced in CGI programming, it will become useful to understand more
about what's happening behind the scenes. Specifically, how the browser and server communicate
with one another. Because although it's all very well to write a program that prints ``Hello,
World.'', it's not particularly useful.
Environment variables are values that float around you as you use your computer. They are
useful things like your path (where the computer searches for a the actual file implementing a
command when you type it), your username, your terminal type, and so on. For a full list of
your normal, every day environment variables, type env at a command prompt.
During the CGI transaction, the server and the browser also set environment variables, so
that they can communicate with one another. These are things like the browser type (Netscape,
IE, Lynx), the server type (Apache, IIS, WebSite), the name of the CGI program that is being
run, and so on.
These variables are available to the CGI programmer, and are half of the story of the
client-server communication. The complete list of required variables is at
http://hoohoo.ncsa.uiuc.edu/cgi/env.html
This simple Perl CGI program will display all of the environment variables that are being
passed around. Two similar programs are included in the cgi-bin directory of the
Apache distribution. Note that some variables are required, while others are optional, so you
may see some variables listed that were not in the official list. In addition, Apache provides
many different ways for you to
add your own
environment variables to the basic ones provided by default.
#!/usr/bin/perl
print "Content-type: text/html\n\n";
foreach $key (keys %ENV) {
print "$key --> $ENV{$key}<br>";
}
Other communication between the server and the client happens over standard input (STDIN)
and standard output (STDOUT). In normal everyday context, STDIN
means the keyboard, or a file that a program is given to act on, and STDOUT
usually means the console or screen.
When you POST a web form to a CGI program, the data in that form is bundled up
into a special format and gets delivered to your CGI program over STDIN. The
program then can process that data as though it was coming in from the keyboard, or from a
file
The ``special format'' is very simple. A field name and its value are joined together with
an equals (=) sign, and pairs of values are joined together with an ampersand (&).
Inconvenient characters like spaces, ampersands, and equals signs, are converted into their
hex equivalent so that they don't gum up the works. The whole data string might look something
like:
name=Rich%20Bowen&city=Lexington&state=KY&sidekick=Squirrel%20Monkey
You'll sometimes also see this type of string appended to the a URL. When that is done, the
server puts that string into the environment variable called QUERY_STRING. That's
called a GET request. Your HTML form specifies whether a GET or a POST
is used to deliver the data, by setting the METHOD attribute in the FORM
tag.
Your program is then responsible for splitting that string up into useful information.
Fortunately, there are libraries and modules available to help you process this data, as well
as handle other of the aspects of your CGI program.
|