CGI Tutorial
CGI (Common Gateway Interface) is a protocol that can be considered an extension to the HTTP protocol. Initially, the HTML language, used to write Web pages, worked only in one direction. It allowed to retrieve documents from a server, but not to send data to it. Soon it was seen the need for letting the user send data as well, for example, when registering at a database. Since version 2 of HTML a mechanism has existed to present users with a form where to input data.
Correspondingly, a protocol was developed (CGI) that allowed data to be processed in the server. CGI also enabled the retrieving of so-called "dynamic pages," that is, pages that are not stored in the server as files but are created on the fly instead. All of this is accomplished by letting a Web page to start the execution in the server of a script (called a CGI script), which may store the user's data in the database or build a page with data retrieved from the database. Or it may show you the statistics of your site! In fact, a CGI script may return any object that a browser can understand: a whole page or any of the elements usually included in a page as, for example, a picture.
The CGI protocol defines two ways by which data may arrive to the script: the GET method and the POST method. The POST method is usually used with forms and the GET method in any other case. While the GET method may be used to send form data, it is impractical when the form is a lengthy one. The reason is that using this method data are sent following a question mark after the name of the script. When there are too much data to send, one can easily exceed the limitations of this type of transfer. This problem does not arise with the POST method, which is therefore preferred for form processing.
The search boxes of the search engines like Yahoo or Google are a special case of form processing where the GET method is mandatory. This method is used so that, when you create a bookmark referring to the dynamic page containing the search results (SERP), the search arguments are stored as a part of the bookmark.
In the case of a form the name of the script is included in the ACTION attribute of the FORM tag. In any other case it is included in an otherwise normal hyperlink. Suppose that you want to run a script called "treasure.pl" with the data "key" taking the value "jim". You would code the following hyperlink:
<a href="treasure.pl?key=jim"> Open the treasure </a>
As you can see, both the name and the value of the data are included. The script receives this pair (as well as abundant information sent by the Web server to every CGI script) using what are called "environmental variables".
Previous | Contents | Next
|