7.4 CGI-Scripts: A way to improve WWW
The Common Gateway Interface (CGI) is a standard for interfacing external applications with information servers, such as HTTP or Web servers. A plain HTML document that the Web daemon retrieves is static, which means it exists in a constant state: a text file that does not change. A CGI program, on the other hand, is executed in real-time, so that it can output dynamic information.
For example, let's say that you wanted to `hook up' your favorite database to WWW, to allow people from all over the world to query it. Basically, you need to create a CGI program that the Web daemon will execute to transmit information to the database engine and then to receive the results back again and display them to the client. This is an example of a gateway, and this is how CGI started.
The database example is a simple idea, but most of the time rather difficult to implement. There is really no limit to what you can hook up to the Web. The only thing you need to remember, is that whatever the CGI program does, it should not take too long to process. Otherwise, the users have to stare too long at their browsers waiting for something to happen.
Since a CGI program is executable, it is basically the equivalent of letting the world run a program on your system, which is not the safest thing to do. Therefore, there are some security precautions that need to be implemented when it comes to using CGI programs. Probably the one that will affect the typical Web user the most is the fact that CGI programs need to reside in a special directory, so that the Web server knows to execute the program rather than just display it to the browser. This directory is usually under direct control of the webmaster, prohibiting the average user from creating CGI programs. There are other ways to allow access to CGI scripts, but it is up to your webmaster to set these up for you. This directory is called cgi-bin. A CGI program can be written in any language that allows it to be executed on the system, such as:
- any UNIX shell
- Perl
- Visual Basic
- AppleScript
- C/C++
It just depends what you have available on your system. If you use a programming language like C or Fortran, you know that you must compile the program before it will run. If you look in the cgi-src directory that came with the server distribution, you will find the source code for some of the CGI programs in the cgi-bin directory. If, however, you use one of the scripting languages instead, such as Perl, TCL or a UNIX shell, the script itself only needs to reside in the cgi-bin directory since there is no associated source code. Many people prefer to write CGI scripts instead of programs, since they are easier to debug, modify and maintain than a typical compiled program.
