The CGI/PERL Cookbook

The CGI/PERL Cookbook

by Craig Patchett, Matthew Wright


$45.94 $54.99 Save 16% Current price is $45.94, Original price is $54.99. You Save 16%.

Product Details

ISBN-13: 9780471168966
Publisher: Wiley
Publication date: 10/24/1997
Series: Cookbooks Series , #2
Edition description: BK&CD-ROM
Pages: 656
Product dimensions: 7.44(w) x 9.37(h) x 1.44(d)

About the Author

CRAIG PATCHETT is a professional programmer and experienced computer book author.

MATTHEW WRIGHT is the creator of the popular "Matt's Script Archive" and "CGI Resource Index" Web sites.

Read an Excerpt

Chapter 3: Installing CGI Programs

The programs and subroutines written for this book are provided in a ready-to-run format that, in most cases, only require you to modify a few configuration variables before they'll work on your server. You do need to install them first, however, and while each program chapter includes its own section covering installation details specific to that particular program, there are some general procedures that apply to them all. This chapter will go over those procedures with you and walk you through the process of installing a generic CGI program on your server. If you're already familiar with this process, then you can skip this chapter and go right to the first program in Chapter 4 (or to whichever program interests you most).


Many people who haven't used CGI programs before (or who have used programs that were installed by someone else) seem to have the misconception that installing one requires some kind of technical degree. This probably originated with early CGI programmers who wanted to protect their consulting income by making the whole process seem a lot more complicated than it really is. The truth is that installing a CGI program requires three simple steps, regardless of what operating system and Web server you're using:

    1. Copy the program to your server.

    2. Move the program into the appropriate directory.

    3. Make sure your server knows about and can access the program.

That's pretty much all there is to it. The programs in this book also depend on some required support files that need to be copied to the server, but the process for doing so uses these same three steps. Since the exact implementation of these steps varies from platform to platform, we've included specific instructions for Unix, Windows, and MacOS servers.

A Few Assumptions

Because we have a limited number of pages to fill and a lot of information to pack into them, we have had to make a few basic assumptions for this chapter regarding your familiarity with your Web server. If any of these assumptions don't hold true for you, then you can rectify the situation by asking your ISP's system administrator a few questions or by consulting your server's documentation if you're running your own server.

You Have Access to a Web Server

This may sound like an obvious assumption, but some people don't realize that you cannot run a CGI program unless it is installed on a Web server. This means that either you have an account with an Internet Service Provider (ISP) that allows you to install Web pages and CGI programs on their server, or that you own or have access to a computer that is connected to the Internet and running Web server software. (Craig used CTSNet at during the development of the book and Matt used World Wide Mart at All of the code has been tested on their servers already, so if you need to find an ISP we highly recommend both.)

For debugging purposes it is also helpful (although not required) if you have telnet access to a shell account on your server. This allows you to test the programs on the server itself rather than testing them on your local computer. If you're not sure whether or not you have telnet access, check with your ISP.

Perl Is Installed on Your Server

Although we have provided copies of Perl for a variety of platforms on the accompanying CD-ROM, it is beyond the scope of this book to cover the installation process. We will therefore assume that you are either using a server that already has Perl installed or that you are capable of installing Perl yourself from the CD-ROM (Readme files should be included with each version that explain the process.) If you're using a Unix-based server, then we also assume that you know where the Perl executable is located on your server. You can find out by checking with your system administrator or by using one of the following commands from the Unix shell prompt (some servers may not support these commands):

which perl
where is perl

You Have Access to a CGI Directory

Depending on the type of server you're using, the platform it is running on, and the way it has been configured, CGI programs may need to reside in a special directory in order to run. (Some servers allow you to keep CGI programs anywhere on the server's Web space as long as the filename ends with . cgi.) Traditionally this directory is named cgi-bin (especially on Unix servers), but it can have any name as long as the server is configured to recognize it. The process for doing so varies from server to server but in all cases must be performed by the system administrator. If you are using an ISP for your Web account, then you will need to ask the system administrator where to install your CGI programs. Otherwise please refer to your server software's documentation for instructions on how to configure a directory for CGI access.

NOTE: You cannot simply create a directory named cgi-bin, place a CGI program in it, and expect it to run. Your server must be configured by the system administrator to recognize a directory before it can be used for CGI access.

You Know How to Transfer Files to Your Web Server

Unless you have access to your Web server through your computer's file system, you will most likely need to transfer the programs in this book from the accompanying CD-ROM (or our Web site) to your server using an FTP program. We assume that you already know how to do this and have probably done so with HTML pages in the past. If not, you will need to get instructions from your ISP on how to go about it. (It's easy.)

On the CD-ROM, you'll notice that we provide Unix, Windows, and MacOS versions of each program and their supporting files. These versions differ only in the characters used to terminate each line in the file. (Each platform is different.) If you are transferring files directly from the CD-ROM, then make sure you transfer them from the directory that corresponds to the platform your server is running on. If you open and save these files on a different platform than the one your server is running on (i.e., your server is on a Unix machine and your home computer is a Mac), make sure you edit the version for your server's platform and that your editing program doesn't change the line breaks when it saves the file. You should also make sure that when you transfer the files to your server, you do so in ASCII mode as opposed to binary mode. (See the instructions for your FTP program if you're not sure how to do this.) In many cases this will take care of any line break problems.

Installing CGI Programs

Thanks to the standardized CGI protocol, the difference in the way CGI programs are installed on different platforms is small, as you will see in the following summaries.

Unix Servers

Installing a CGI program on a Unix-based server is a little more involved than on other platforms, but it is still a relatively straightforward process. Once the program has been copied into the appropriate directory, the only remaining step is to make sure that the program file has the correct access permissions (these dictate who can read from, write to, or execute the file).

Setting File Permissions

File permissions are set using Unix's chmod, command, the format for which is as follows:

chmod permissions filename

permissions is a three-digit number that indicates the type of permissions to give to the file. This number confuses a lot of people who aren't used to Unix, but it is actually very easy to understand once you know what it means.

The first digit defines what permissions the owner of the file has (i.e., the person who created the file), the second dictates what permissions the group that the owner belongs to has, and the third dictates what permissions everyone else has. Each category of permission (read, write, execute) is assigned a numeric value. Read permission is 4, write permission is 2, and execute permission is 1 (execute permission for a directory means that the directory can be searched). If you want the owner of the file to have permission to read, write, and execute the file, then you use a 7 (4 + 2 + 1) for the second digit. If you want the group to have permission to read and execute only, then you use a 5 (4 + 1) for the third digit. An example chmod command that gave full permissions to the owner and read and execute permissions to the group and everyone else would look like this:

chmod 755 filename

Typically these (7 5 5) are the permissions settings you will want to give to your CGI programs. (Required, template, and data files don't need to be executable, so you will use 644 with them.) You may also want to create an empty index.html file in the same directory as you CGI programs and set its permissions to 7 00. This will prevent people from getting access to your programs by using the URL of your CGI directory.

NOTE: Don't forget to set the access permissions for any directories or subdirectories you create when setting up your program. Otherwise you will not be able access your files properly, even if their permissions are set correctly.

Running the Program

Once your program is in the right directory with the file permissions set properly, it should be all set to run. You can test it by entering the URL of the directory it is saved in (check with your system administrator if you don't know what this URL is), followed by the name of the program file, into your browser.

Windows Servers

The installation process for Windows-based servers is the simplest of the three. The process of installing the Windows version of Perl automatically associates the cgi and pl extensions with the application, so your programs will be executable as soon as they are copied to your server's hard drive. The only extra step you need to worry about is copying them into a directory that is accessible by the server. If you are not sure what directories this includes, you should check with your system administrator. Once the program is in a valid directory, you can run it by opening the URL in our browser

MacOS Servers

The installation process for MacOS-based servers is also very straightforward. just follow these steps for each CGI program you want to install (required files can simply be copied to the appropriate directory):

    1. Run MacPerl and open the CGI program.

    2. Select Save As... from the File menu.

    3. Select CGI Script from the popup, menu at the bottom of the Save dialog box.

    4. Name and save the file.

This procedure will create an executable file that can be called the same as any other CGI program. At this point you can test the program by entering its URL into your browser's location input area.

NOTE: If you have an older version of MacPerl and don't see CGI Script as a choice in step 3, you will need to either update the version you're using or download the PCGI package from ftp:// and copy the file CGI Script into the MacPerl Extensions folder in the MacPerl folder.

Installing Required Files

Regardless of what platform your server is running on, required files can be installed in any directory as long as you set the $REQUIRE_DIR configuration variable in each program to point to that directory. If your server is Unix-based you will also need to make sure the permissions are set correctly for the directory and the required files.

Installing Support Files

Most of the programs in the book run in conjunction with several supporting files, which may include HTML files, template.files, and/or configuration files. A suggested directory structure (along with the appropriate file and directory permissions for Unix users) for each program is included in the Installation section of the corresponding chapter. Sample support files are included for each program on the accompanying CD-ROM.


There are several things that can go wrong in the process of installing CGI programs, and even the most seasoned CGI users make mistakes every now and then. The three most common installation errors will cause your Web server to generate an error message that will be displayed in your browser. Each of these errors is defined in the following sections, along with the steps you'll need to take to remedy the situation.

"403 Forbidden" Error

This message indicates that the file permissions for the program are probably set incorrectly. Remember to set the permissions for the directory and/or subdirectory your program file is in as well as for the program file itself.

"404 Not Found" Error

If you get this message after attempting to access a program, then you've probably entered an invalid URL for the program. Double check the URL you're using and make sure you have specified the correct name and location of the program file. (Remember that uppercase letters are different than lowercase letters to Unix.)

"500 Server Error" Error

This error indicates that the program is not producing output that the server understands and generally indicates a problem with the program itself. You can check your server's error log for a more detailed explanation of the problem (check with your system administrator to find out where the error log is kept). If you get this error with one of the programs in this book, then there is probably a problem with one of the configuration variables in the program. Double-check each variable to make sure you didn't accidentally omit a closing quote or semicolon at the end of the line. Then try some of the debugging techniques presented in Appendix A to see if you can locate the problem. If not, please check the book's Web site to see if anyone else has had the same problem and/or let us know about the problem by filling in the bug report at the book's Web site ( We'll do our best to take care of it as quickly as possible.

Table of Contents


Using This Book.

Using CGI and Perl.

Installing CGI Programs.


Managing Files: Fileseek.CGI.

Gathering Input: Formhandler.CGI.

Spreading the Word: Broadcaster.CGI.

Doing Business: Webshop.CGI.

Getting Opinions: Feedback.CGI.

Keeping Track: Pagecontrol.CGI.

Restricting Access: Authenticate.CGI.

Getting Support: The Subroutines.




Customer Reviews

Most Helpful Customer Reviews

See All Customer Reviews