Make Your Form Work with CGI

What is CGI and why do you need it? CGI stands for the Common Gateway Interface, a fancy name that describes another protocol. If you remember from Chapter 5, a protocol refers to how information is exchanged on the Internet. CGI is a protocol that deals with the exchange of information from your visitor’s Web browser (the client) to your Web host (the server) and, ultimately, to you (the Webmaster). The best way to understand why you need CGI is to learn what CGI does.

Understand CGI

As was mentioned earlier in this chapter, XHTML is wonderful, but is limited in its scope. Even though XHTML can collect information through forms, it was never intended to process that information. For that, you need a program (generally called a script) that resides on the Web server in a special location (usually what’s known as a cgi-bin). When a visitor (client) fills out your form and clicks Submit, the information on the form is sent by the client browser to your cgi-bin, where the script processes the data and sends it on to you, usually by e-mail. The script can be written in one of several different programming languages, but usually it’s done in a language called Perl. Is this beginning to sound complicated? It can be. That’s why some beginning Webmasters might want to consider alternatives to using CGI.

Consider Alternative Form Processing

If you want to collect information from your visitors but don’t want to deal with the headaches of using the Common Gateway Interface, there are some alternatives. Unfortunately, they all tend to have their own problems. Some possible ways to avoid CGI include the following:

  • Avoid forms altogether. If your site does not depend on collecting information from your visitors, don’t worry about CGI. Remember, just because you can do something with XHTML doesn’t mean you have to do it. Forms are like any other element of Web design: If you don’t need them, don’t bother with them.

  • Use the mailto: protocol. At the beginning of this chapter you learned how to have form data e-mailed to you with the mailto: protocol. The main drawback to this approach is that not all browsers support it. Thus, you might exclude some of the visitors who want to use your forms. Another issue is security. When someone sends you information this way, they reveal their e-mail address to you. In these security-conscious days, people are increasingly hesitant to allow personal information to be revealed to someone they do not know. The following illustration shows the prompt that comes up when a visitor clicks Submit on a mailto: form:

    click to expand

  • Locate a form-processing service. If you are willing to pay a fee, there are services that will handle your forms for you. In this case, all you have to do is link to them, and all the form processing is handled from their end. If forms are a must for you, you might consider this an option. You can find them by doing a simple search on Google or another search engine using the keywords “form hosting.” Table 14-1 lists some free and fee-based form-processing services you might want to explore.

Table 14-1: Form Processing Servces

Form-Processing Service


My Secure Form (If you need a secure or encrypted form server)

Response-o-Matic Free Form Processor


TipJar Generic Form Handler (Free) HTML Form Builder



Bummer Form Hosting

DataTrend Software CGI Scripts

If these options don’t sound appealing to you or if you just like a challenge, put on your waders and step into the sometimes-intimidating waters of the Common Gateway Interface.

Understand CGI Form Processing

If you already know how to program and can understand how to work in Perl or a similar language, you’re way ahead of the game. However, knowing how to program isn’t absolutely necessary as long as you know what kind of script you need, where to look for it, and how to implement it. So if the thought of CGI and programming in Perl makes you break out in a cold sweat, relax. You don’t have to write the script; you just have to find it and install it.

Find a Script

To figure out what kind of script you need to make your form work properly, you must ask yourself what you want the form to do. Is it a guestbook form? If so, you need to look for a guestbook script. Is it an online catalog or order form? Look for a shopping cart script. Is it a survey or general information form? If so, you will want a simple form script.

There are many sources for free CGI scripts on the Web. Although these scripts are generally copyrighted, most of the time the only requirement is that you leave the header portion of the script (which contains information about the author) untouched. Others might not want you to make any modifications to the script (other than what is necessary to make it functional for your pages). A handful of the sources for CGI scripts are listed in Table 14-2.

Table 14-2: Online Sources for CGI scripts

Script Source


The CGI Resource Index

Dream Catchers Free CGI Scripts

Free CGI


Matt's Script Archive

TuCows (Search for “cgi scripts”)

Free Dynamic CGI Scripts


There are many different kinds of CGI scripts. As you explore some of these script archives, you might decide to have some fun experimenting with more than just form processing.

When you have found the script that meets your needs, download it and unzip it (if necessary). Depending on the kind of script you are going to use, you might find that you have been given a CGI script, written in Perl (, perhaps one or two HTML files, and usually a ReadMe file. The ReadMe file has the instructions you need to get your script up and running. When you have sorted out your files, you are ready to begin phase two of your CGI experience: configuring the script.

Configure Your Script

Obviously, a generic script is not likely to fit your form perfectly. After you have downloaded your script, you will need to tailor it to meet the needs of your form. In some cases (as with a form script), you might also need to tweak your XHTML file, or copy the code from the file you downloaded into your own page. It all depends on what your script needs to work properly.

Because every script is different, and your forms and Web accounts will be unique to you, it’s difficult to give precise instructions for configuring a script. That is why the ReadMe file included with your script is essential to setting it up properly. You also might need to contact your Web host to find out about how to use CGI on its server.

Another part of the configuration process that might or might not be necessary is that of adjusting the form, guestbook, and so on, to meet your needs. Often the scripts you download are zipped files that include the basic Web pages you need to execute them. However, you probably will prefer to custom-tailor the Web page to fit the look of your site. Most of the time that can be done by either modifying the page directly, cutting and pasting the relevant code into one of your own pages, or by linking the page to a style sheet you have created. After you have done that, you can turn a plain form page into a customized page that fits the look of your site, as in the illustration that follows:

click to expand

start sidebar
Did You Know?—Not All Web Hosts Support CGI

Because CGI raises some security issues, some servers do not support it. It's important to check with a prospective Web host to learn whether you will be allowed to use CGI. For example, Yahoo Geocities free server does not allow you to use CGI scripts. If forms and other server-side applications will be important to your site, be sure to find out whether you can use CGI before you commit your funds to setting up a site on a particular server.

end sidebar

Upload Your Files

When you have configured your script (and your form, if necessary), the next step is to upload the files to your server. This is the easiest part of the whole process. You use your FTP program just as you would for any of your other Web pages. All you need to remember is that your CGI script needs to go into the cgi-bin directory, and that you should put the other files in the location you specified when you configured the script.

Change Permissions

Perhaps the most confusing—and scary—part of using some CGI scripts is that you might have to change some of the security settings for the files on your server. For example, with a guestbook program it is necessary to configure the server to allow your visitors to leave messages that will be added to a Web page. That means that people who visit your site are given permission to write information onto your server’s hard drive. Are you beginning to grasp the security implications? What if someone with a little know-how decided it would be fun to make some modifications you (or your server) had not planned on? That is why not all servers allow you to use CGI. If you need to change permissions (and, remember, not all CGI scripts require it), you can contact your server’s technical support to find out exactly how to go about it.


CGI guestbooks are easy targets for spammers who load your pages with phony comments and links pointing to commercial—and often pornographic—sites. Unless you plan on checking your guestbook regularly (that is, every few days), you might find it better to use the mailto: protocol for guestbook entries and then post them to your Web site yourself.

Test Your Form

After you have configured your files, uploaded them, and made any changes to the file permissions, you are ready to test your form. Log on to your site, fill out your form (or guestbook), and press Submit. Hopefully, you won’t receive an error message; instead you’ll be celebrating the first successful addition to your Web site’s guestbook.

How to Do Everything with HTML & XHTML
How to Do Everything with HTML & XHTML
ISBN: 0072231297
EAN: 2147483647
Year: 2003
Pages: 126

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: