Printed from TeacherJohn.com • http://teacherjohn.com/tutorials/cgibasics.php

Common Gateway Interface (CGI) Basics

Common Gateway Interface, or CGI, is a way of running scripts on the Web server. Using CGI, you can incorporate many interactive and powerful features into your Web site. CGI is server-dependent, not client-dependent. In other words, unlike CSS or JavaScript, it does not matter what browser the person viewing your page is using. Also unlike CSS, Javascript, or plain old HTML, you cannot test your pages using CGI scripts unless they are on the server.

Most CGI scripts are written in Perl (Practical Extraction and Reporting Language), a programming language. But it is not necessary to know how to program in Perl if you simply want to use an existing CGI script. There are thousands of CGI scripts available on the Internet; many of them are free. With a little knowledge of the basics, you can learn to find, download, configure, install, and use many useful scripts.

CGI scripts are plain text files. You can edit them in any text editor. You must remember to save as text, and upload as text (just like an HTML file, but the extension must be .cgi).

Most Web hosting services have Perl installed on the Web server, which is necessary if you are to be able to run a CGI script written in Perl. Your provider may already have some standard scripts installed for customers to use. In this case you need to find out what scripts are available, and how to execute them.

Some providers allow you to install your own scripts. Please use this privilege responsibly!

Generally the steps involved in utilizing CGI scripts are as follows:

1) Find a CGI script you wish to use. One good place to start is the the CGI Resource Index site at:

http://cgi.resourceindex.com/Programs_and_Scripts/Perl/

2) Download the script you wish to use. Simple scripts may be a single file; more complex scripts may actually consist of several files. Often you will download a single file, or archive, which is compressed with "zip" (Windows), "stuffit" (Macintosh), or "tar" (Unix). After you decompress the archive you may wind up with many files.

3) Read the documentation. Usually there will be a README file that explains how to configure, install, and execute the script. Even if you follow the instructions as carefully as possible, it may be necessary to resort to trial and error until you are successful.

4) Open any files ending with a .cgi extension. The very first line in such a file will be the "path to Perl." Depending where Perl is installed on the server, you may need to change this line.

If the script is a Perl 5 script (assume it is, as Perl 5 has been the curent version of Perl for some time now), the very first line of the script must appear as follows:

#!/usr/local/bin/perl5

If the script is an old Perl 4 script, the very first line of the script must appear as follows:

#!/usr/local/bin/perl

5) Make any other changes as indicated in the documentation. Sometimes there will be a separate file where you make changes to certain variables or settings necessary to customize or run the script.

6) Upload the files to your "cgi-bin" directory. Make sure the files are uploaded as text, not binary. In other words, select the "text" or "ASCII" setting in your FTP software before uploading a CGI script.

7) Using your FTP software, change the file permissions of the CGI script to be executable. It may be necessary to change the permissions of other files; again, see the documentation.

8) You will probably execute the script by using an SSI "exec" directive, or by clicking on a link to the script.

9) If the script runs correctly the first time — congratulations! Probably, however, you will need to take a deep breath and start troubleshooting. Did you remember to change the file permissions?