|
CGI: The Common Gateway Interface for Server-side Processing
-Parikshit Chudasma
The Common Gateway Interface,
or CGI, permits interactivity between a client and a host
operating system through the World Wide Web via the Hyper
Text Transfer Protocol (HTTP). It's a standard for external
gateway programs to interface with information servers, such as
HTTP or Web servers. A plain HTML
document that the Web server delivers is static, which means it
doesn't change. A CGI program, on the other hand, is executed in
real-time, so that it can output dynamic information - perhaps a
weather reading, or the latest results from a database query. CGI
allows someone visiting your Web site to run a program on your
machine that performs a specified task. |
Gateways are programs which handle information requests and return the
appropriate document or generate a document on the fly. Your server can
serve information which is not in a form readable by the client (e.g. an
SQL database), and act as a mediator between the two to produce something
which clients can use.
Gateways can be used for a variety of purposes, the most common being
the handling of FORM requests for HTTP. An HTTP
server is often used as a
gateway to a legacy information system; for example, an existing body of
documents or an existing database application. The Common Gateway
Interface is a convention between HTTP server implementors about how
to integrate such gateway scripts and programs.
Gateway programs, or scripts, are executable programs which can be run
by themselves. They have been made external programs in order to allow
them to run under various (possibly very different) information servers
interchangably.
Gateways conforming to this specification can be written in any
language which produces an executable file. Some of the more popular
languages to use include: C or C++, Perl,
Python, TCL, shells, and many others. It doesn't matter
what language the program is written in, as long as you have the
permission and resources to run it on your machine and the program is
written correctly.
Getting Started
Here is a simple example demonstrating the Common Gateway Interface. This
example uses the Perl
language because of its portability and relative ease of use. When we
explain operating system commands we will generally speak UNIX.
Note that UNIX is CaSe-sEnSiTiVe.
Some servers allow your CGI programs to be anywhere in your web
directories, so long as the file name ends in ".cgi". Others
require you to put them only in the "/cgi-bin" directory. Check
with your system administrator. Now, create a file called Hello.cgi:
#!/usr/bin/perl
$t = "Hello World!";
print <<EOT;
Content-type: text/html
<Title> $t </Title>
<H1> $t </H1>
EOT
- The first line must contain the path to your Perl interpreter. Use
the command "which perl" to check this.
- Scalar variables names in Perl start with the "$"
character. "$t" contains some text to be used later.
- The print statement prints everything following until the "EOT".
Text printed to "standard output" goes to the server and
thence to the browser.
- The first line printed is an HTTP
header to tell the browser that an HTML file is coming.
- HTTP header lines must always be separated by a blank line from
actual data.
- The data sent is a valid
HTML document. The Perl
interpreter replaces "$t" with "Hello World!".
Save the file and exit. Change the attributes of the file to make it
executable: chmod +755 hello.cgi Create a link to it like
this:
<a href="Hello.cgi">Hello.cgi</a>
Click on Hello.cgi,
and if all's well, the script should respond with "Hello World!"
- Forms
- We've seen how a CGI script can send information back to a browser,
but how do we send information to a CGI script? This can be
accomplished using what are referred to as forms. Forms allow for user
defined information to be passed along from a Web browser to a CGI
program for processing.
- Environment
Variables
- Unlike form variables, environment variables are not user defined
but are server defined. These variables are passed along everytime a
CGI script is invoked.
- Imagemaps
- Imagemaps allow users to click on a particular spot in an image to
retrieve another HTML document or to run a specified script.
Examples
- The INPUT
tag: text entry for The URL-Minder. This is the most important
of the form elements; with it you can allow the user to input text or
passwords and submit them to the server for CGI processing.
- The SELECT
tag: Navigation Menus for CGI or JavaScript
processing.
- The TEXTAREA
tag: Comments Feedback to let your visitors send their
comments to an email distribution list.
Selena
Sol's WebWare |
A monthly column for
the cultural anthropologist and other liberal arts hackers gone
Webmaster. |
CGI-Capable
Web Servers |
ServerWatch is the
ultimate guide to Internet servers and Web development tools, and
has information about which Web servers are CGI-capable. |
CGI
Authoring Resources |
If you can't find
what you're looking for, the WDVL's CGI Authoring Resources page
will get you up to speed in no time! |
Introduction
to Web Programming: CGI |
Introduction to Web
Programming is a four half-day course designed by Selena Sol. |
|