What is Common Gateway Interface (CGI)
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.
[br]
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.
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.
[br]
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.
[br]
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.
[br]
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.
[br]
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.
[br]
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.
[br]
The advanced tasks are currently a lot easier to perform in CGI than in Java. Examples of such tasks include Database Backending, Search Engines, and 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.
[br]
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 does CGI Function?
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.
[br]
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.
[br]
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:
- GET Method
- POST Method
- 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.
[br]
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.
[br]
CGI is moderately easy to use, and hence the best all-around choice. CGI has the following features:
- Some advanced form handling.
- can handle all of the CGI environment variables.
- can perform automatic redirection.
- can output HTTP error/status codes instead of a document.
- 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. The following are the features of CGI-HTML:
- It has the most advanced form handling of any of the libraries.
- It can handle all of the CGI environment variables.
- It can perform automatic redirection.
- 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:
- constantly updated.
- contains many neat features.
- very easy to use.
- can handle all CGI environment variables including arrays, matrices, and linked lists.
- contains classes for handling bitmaps and other image types.
- 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.
[br]
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.
[br]
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
[br]
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 to the Web server to be sent back to the client. All of this happens behind the scenes.
[br]
Also, read