CGI
Ron Graham
Common Gateway Interface (CGI) is the methodology used to process forms on the Web. Any HTML form involves an action, and that action is taken by a CGI script or program, manipulating data a visitor to your Web site enters on the form.

CGI is used by many Webmasters to produce the following popular effects:

  • message boards
  • guestbooks
  • hit counters and visitor statistics
  • surveys
  • password-protected pages
  • calendars and clocks
  • adding visitor-supplied links
  • ad rotators
  • shopping carts
  • random images, text, and/or links
some of these effects are considered by their proponents to be useful in e-commerce.

There are two methods that the HTML form can use in processing information: GET and POST. A very rough summary of these methods: both will manipulate the data entered by the user onto the form and present some result in the user's browser; POST will create or modify some file as part of the action.

The CGI program will read form input, which has been organized into "name-value pairs," where "name" is the name of a field in the form, and "value" is whatever the site visitor has entered in that field. The program will separate the pairs from one another, then separate individual name from associated value, then perform operations on the values and display the results of those operations on the visitor's browser.

Special characters entered into form fields will be converted to hex code, and you may need to have your CGI programs parse the hex code and convert back. Here are examples of common characters that need parsing in CGI:

Hex Code Parse To
21 !
26 &
28 (
29 )
2C ,
2F /
3A :
3F ?
40 @
5E ^
7E ~

There are two types of CGI: scripts and programs. Scripts are interpreted by the hosting server "at runtime," or when the HTML form is submitted by the user. Programs are compiled into executable files and run. Though there are many considerations involved in choosing whether to use scripts or programs, these are generally fundamental:

  • What does your service provider allow you to do?
  • What do you like better?

What a service provider allows can be critical. Many system administrators recognize that "a rogue CGI program can take down the entire Web site," as put by the administrators at TCNJ. Some service providers won't allow user-supplied scripts at all; others only allow their own. America Online, for instance, has two CGI scripts: one for a guestbook and the other for sending template e-mail; those scripts can't be modified by any AOL subscriber -- and even those can be victimized by malicious spammers.

There are at least two ways that CGI can compromise site security:

  • through allowing users to submit system commands without authorization
  • through allowing users to submit text or HTML without size and/or repetition restrictions
If you are creating your own custom CGI, you may find it necessary to bypass these problems by limiting user response.

Even more important than security considerations, however, is the knowledge that many of the most popular effects for the average user already exist, are available as freeware or shareware, and require only modest customization to be ready to use.

If you create your own CGI, you should test it thoroughly before releasing it to the public. Even having done so, someone will always find a creative way to make the script fail; and CGI will not usually be very good at telling users why it fails, unless you've included some exhaustive error handling in your code. This can be another good reason to put limitations on what data users can enter on your forms.

Since your CGI runs on your own server, you must either know exactly where your service provider carries Perl, or compile your C program on that server. Other CGI programming methods require similar precautions.

References

I'm having a hard time finding a good book on CGI programming with C or C++. And I'm ignoring programs other than Perl and C/C++ on the basis of programmer popularity for the time being -- though I hear good things particularly about Tcl.

Guelich, et. al. CGI Programming with Perl. O'Reilly & Associates, 2000. ISBN 1-56292-419-3
Hamilton, J. CGI Programming 101. cgi101.com, 2000. ISBN 0-96694-260-4
http://www.cgi-resources.com/ -- free CGI scripts in Perl or C/C++; free hosting
Matt's Script Archive -- a large supply of useful free scripts, mostly in Perl
Jukka Korpela's Getting Started with CGI Programming in C

POSSIBLE FREE CGI HOSTING SITES

www.cgi-free.com
www.terminalp.com/free-cgi/
www.free-cgi.com
www.remote-software.com
www.lpage.com
www.digitmania.holowww.com
www.siteflow.com/hvb2e/create2.html
www.interactive-web.net
www.insidetheweb.com
free.prohosting.com
www.hostedscripts.com


What you can do
  1. Make sure your application doesn't re-invent the wheel. You may find it economical to use a prepared script, or one remotely-hosted by someone else.
  2. If you are going to prepare your own scripts, test them as much as you can, and provide error handling.
  3. Make sure you understand your service provider's policy on CGI, and how to load and access your scripts.

[Table of Contents] [Previous] [Next]