The Common Gateway Interface (CGI) is an accepted standard for interfacing Web servers and external applications. Web servers and external applications. Web servers were originally designed to serve static HTML documents along with other associated static files. A WEb browser that communicates with a Web server limits its functionality to serving static pages. it displays only documents whose contents will not change between requests or during page visualization. However, CGI established a standard for information to be exchanged between Web servers and browsers. CGI allows the passing of information between a browser or server to an external program that performs some actions. it then outputs its results back to the user's browser.

What is Common Gateway Interface (CGI)

CGI (Common Gateway Interface) is a standard whose specification defines a way for Web servers to communicate with external programs, and vice versa. with this method, the external program can generate HTML, images, etc. for the server for its processing.


Why CGI?


CGI is used to generate dynamic content with the same ease that you generate static content. it is a very well-defined and supported standard, and without CGI, dynamic content would have been impossible without proprietary server methods. There are many useful applications of CGI programs.

An example of a simple task would be a hit counter. these are CGI scripts are written in either Perl, C, or maybe just a simple shell script, but they all basically work the same way. they keep a single data file that stores the number of hits. the program reads that data file and when called, increments the number by one and then returns that number. the code to counter can be found at ftp://server.berkeley.edu/pub/www/counter.

CGI is obviously not the only method for creating counters, but as things stand today, it is the best, for three main reasons. the first, because it is currently the quickest. The second reason is that the CGI standard is the most compatible with today's browsers. CGI is a technology that interfaces with HTML. CGI applications generate HTML or images. The third reason is availability. CGI-based counters and CGI code to perform simple tasks are available in plenty.

It is always easier to use code already written than it is to write your own. This (the prewritten code) is a major factor as to why simple tasks are coded in CGI. They are already done by someone else, usually include sample documentation and commented source code, and are waiting for you to just implement them on your server.

The next stage of tasks is the creation of image maps and animation. While basic image mapping requires no programming when done on the client-side, doing something more than just jumping to an HTML page when you click on an image requires programming. CGI uses up a lot of processing time. Java consists of real, live applications embedded into the Web page and is therefore fully interactive.

The advanced tasks are currently a lot easier to perform in CGI than in Java. Examples of such tasks include Database Backending, Search Engines, Multiple Dynamic Pages. CGI programs can do many things on many platforms. In fact, one of the major features implemented in servers supporting CGI specification is that the programs can be written in any language and on any platform as long as they conform to the specification. By comparison, most server extensions are proprietary and specific to one server software, one platform, or both. This means that it is not specifically tied down to the PC, as is VBScript.


Scope of CGI


Basic CGI applications are extremely easy in C, C++, Shell Scripting, and Perl. CGI's usefulness ends on the Web page. After you leave the idea of generated documents that do not change after they are generated (in terms of that specific instance of loading of page), CGI is worthless. Real live applications embedded on Web pages are the realm of languages such as ActiveScript and Java.

The usefulness of CGI is a standard to programming because it varies from task to task. The other point is that CGI is a relatively old technology. But, as newer technologies, such as the Java programming language grows up, they may eventually make CGI absolute.


How CGI Functions?


Imagine that you have a product database on your system that you would like users on the Web to use, but your Web server does not understand the database internals. You must link both the Web server and the database by using a CGI program. This special-purpose program may be developed by you or provided by the database vendor and will be responsible for the database queries on one hand, and the communication with the Web server on the other hand. This last functionality works only because the Web server and the program have established rules for communication between the two. The rules that make them able to interface are called the Common Gateway Interface.

The Web Servers are usually with a set of general-purpose CGI programs that reside in a CGI-bin directory, within the Web serve root directory. This is the directory commonly used for CGI program storage, but the Webmaster can define other locations. CGI applications can be written in any language that can be executed on a computer, particularly a Web platform. In fact, you can choose any of the common languages for your CGI applications.

Your choice depends on what you have to do because different languages may be specialized for different purposes. Perl, for instance, is great for string and file manipulation, while C is better for bigger, more complex programs. Perl and C are probably the most used languages for CGI programming.


CGI Methods


A method is a way of invoking a CGI program. There are three main methods. These are:
  1. GET Method
  2. POST Method
  3. HEAD Method

GET Method

In the GET method, the CGI program receives that data in the QUERY_STRING environment variable. The program must process the string to interpret the data and execute the needed actions. The GET method should be used when you want to obtain data from the server and you will not change any data on the server.

POST Method

When you use the POST method, the Web server transmits the data to the CGI program through the stdin (standard input). The server does not mark the end of the data with an EOF character, so the program must use the CONTENT_LENGTH value to read the stdin correctly.

You should use the POST method when the data you send will alter any data on the Web server or when you want to send large amounts of data to the CGI program (usually, more than 1024 bytes, the length limit of a URL).

HEAD Method

The HEAD method is similar to the GET method, except that with the HEAD method, only the HTTP headers (and not the data itself) are sent by the Web server to the browser.


CGI Libraries


There are many languages used for CGI programming. with each of these different languages, there is almost always more than one library for CGI.

CGI Libraries for C Programming.

CGI libraries for the C language (CGI) are the best to be used for the following reasons as they
are actively maintained,
have great documentation,
have good support.

CGI is moderately easy to use, and hence the best all-around choice. CGI has the following features:
  1. Some advanced form handling.
  2. can handle all of the CGI environment variables.
  3. can perform automatic redirection.
  4. can output HTTP error/status codes instead of a document.
  5. allow capturing CGI sessions for later playback/debugging as well as a full features error handling system.

CGI-HTML Library

It is still in major development and does not offer priority support. it offers source code, and it is completely free. Following are the features of CGI-HTML:
  1. It has the most advanced form handling of any of the libraries.
  2. It can handle all of the CGI environment variables.
  3. It can perform automatic redirection.
  4. It can output HTTP error/status codes instead of a document.

The C++ CGI Class Libraries

An HTML is one C++, CGI class library. Following are the features of A HTML:
  1. constantly updated.
  2. contains many neat features.
  3. very easy to use.
  4. can handle all CGI environment variables including arrays, matrices, and linked lists.
  5. contains classes for handling bitmaps and other image types.
  6. can output HTTP error/status codes instead of a document.
The documentation for this library is written for programmers, but it does include examples for every class.


Using CGI for Web site Linking


One of the most useful applications of the Web is its capability to link a Web site with a database so Web surfers can search for information. In essence, the Web page becomes the front-end for database applications, enabling you to select search criteria and execute even complex searches of a database that resides on the host computer.

Linking a Web site to a database can be relatively simple. The database can take just about any form and can be as simple as FileMaker Pro data as or as complex as an Oracle SQL database. The bridge that brings together Web sites and databases is the CGI. One well-known example of linking between websites and database is the popular Yahoo! Web site. The Yahoo! site serves as a front-end to an extensive database of Web site descriptions, which can be searched according to keywords.

On the client-side of the database, you see a Web page that includes a form in which you enter your search terms. By executing the search, you launch a CGI script that sends a search command to the Web server in the form of a link to the CGI bin on the Webserver. So a search on the Yahoo! site for public relations firms looks like the following:
http://search.yahoo.com/bin/search?p=the+computer+science

When the Web server receives this URL, it identifies the URL as a trigger for a CGI script and passes it along with the search criteria to the mini-program using CGI. The CGI script then sends the search to the database, receives the results of the query along with the HTML page created by the database to contain the result, and passes it on the Web server to be sent back to the client. All of this happens behind the scenes.