Common Gateway Interface


common gateway interface

[‚käm·ən ‚gāt‚wā ′in·tər‚fās] (computer science) A protocol that allows the secure data transfer to and from a server and a network user by means of a program which resides on the server and handles the transaction. For example, if an intranet user sent a request with a Web browser for database information, a CGI program would execute on the server, retrieve the information from the database, format it in HTML, and send it back to the user. Abbreviated CGI.

Common Gateway Interface

(World-Wide Web)(CGI) A standard for running externalprograms from a World-Wide Web HTTP server. CGIspecifies how to pass arguments to the program as part ofthe HTTP request. It also defines a set of environment variables that are made available to the program. Theprogram generates output, typically HTML, which the webserver processes and passes back to the browser.Alternatively, the program can request URL redirection. CGIallows the returned output to depend in any arbitrary way onthe request.

The CGI program can, for example, access information in adatabase and format the results as HTML. The program canaccess any data that a normal application program can, howeverthe facilities available to CGI programs are usually limitedfor security reasons.

Although CGI programs can be compiled programs, they are moreoften written in a (semi) interpreted language such asPerl, or as Unix shell scripts, hence the common name"CGI script".

Here is a trivial CGI script written in Perl. (It requiresthe "CGI" module available from CPAN).

#!/usr/bin/perluse CGI qw(:standard);

print header, start_html,h1("CGI Test"),"Your IP address is: ", remote_host(),end_html;

When run it produces an HTTP header and then a simple HTMLpage containing the IP address or hostname of the machinethat generated the initial request. If run from a commandprompt it outputs:

Content-Type: text/html; charset=ISO-8859-1

-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">Untitled Document

CGI Test

Your IP address is: localhost

The CGI program might be saved as the file "test.pl" in theappropriate directory on a web server,e.g. "/home/httpd/test".

Accessing the appropriate URL, e.g.http://acme.com/test/test.pl, would cause the program torun and a custom page produced and returned.

Early web servers required all CGI programs to be installed inone directory called cgi-bin but it is better to keep themwith the HTML files to which they relate unless they are trulyglobal to the site. Similarly, it is neither necessary nordesirable for all CGI programs to have the extension ".cgi".

Each CGI request is handled by a new process. If the processfails to terminate for some reason, or if requests arereceived faster than the server can respond to them, theserver may become swamped with processes. In order to improveperformance, Netscape devised NSAPI and Microsoftdeveloped the ISAPI standard which allow CGI-like tasks torun as part of the main server process, thus avoiding theoverhead of creating a new process to handle each CGIinvocation. Other solutions include mod_perl and FastCGI.

Latest version: CGI/1.1.

http://hoohoo.ncsa.uiuc.edu/cgi.