A plain HTML document that the Web server delivers is static, which means it doesn’t change. A CGI program, on the other hand, is executed in real-time, so that it can output dynamic information – perhaps a weather reading, or the latest results from a database query. CGI allows someone visiting your Web site to run a program on your machine that performs a specified task.
Gateways are programs which handle information requests and return the appropriate document or generate a document on the fly. Your server can serve information which is not in a form readable by the client (e.g. an SQL database), and act as a mediator between the two to produce something which clients can use.
Gateways can be used for a variety of purposes, the most common being the handling of form requests for HTTP. An HTTP server is often used as a gateway to a legacy information system; for example, an existing body of documents or an existing database application. The Common Gateway Interface is a convention between HTTP server implementors about how to integrate such gateway scripts and programs.
Gateway programs, or scripts, are executable programs which can be run by themselves. They have been made external programs in order to allow them to run under various (possibly very different) information servers interchangably.
Gateways conforming to this specification can be written in any language which produces an executable file. Some of the more popular languages to use include: C or C++, Perl, Python, TCL, shells, and many others. It doesn’t matter what language the program is written in, as long as you have the permission and resources to run it on your machine and the program is written correctly. The programming language Perl is well known as a language used for CGI, but one of the points of CGI is to be language neutral. The Web server does not need to know anything about the language in question.
An example of a CGI program is the one implementing Wikipedia: you hand it the name of an entry, and it will retrieve the source of this entry’s page (if one exists), transform it into HTML, and send the result back to the browser. Or tell it that you want to edit a page. All Wikipedia operations are managed by this one program.
The way CGI works from the Web server’s point of view is that certain locations (e.g. http://www.wikipedia.com/wiki.cgi) are defined to be served by a CGI program. Whenever a request to a matching URL is received, the corresponding program is called, with any data that the client sent as input. Output from the program is collected by the Web server, augmented with appropriate headers, and sent back to the client.
Because this technology generally requires a fresh copy of the program to be executed for every CGI request, the work load quickly overwhelmed many servers and more efficient and flexible technologies such as PHP were developed by 2000.
The complete list of CGI variables is at http://hoohoo.ncsa.uiuc.edu/cgi/env.html