Common Gateway Interface (CGI)

Last Edited

by

in

CGI, or Common Gateway Interface, is a standard mechanism for communication between Web servers and server-side gateway programs that was developed in the UNIX networking environment.

What is CGI Gateway Interface?

CGI Gateway, Common Gateway Interface, is a standard mechanism for communication between Web servers and server-side gateway programs that was developed in the UNIX networking environment.

CGI - Common Gateway Interface
CGI – Common Gateway Interface

These gateway programs are written either in a compiled language such as C or in an interpreted language such as Perl. Common Gateway Interface (CGI) allows Web servers to run scripts or programs on the server and send the output to the client Web browser.

How It Works: Common Gateway Interface

When a Web client such as Microsoft Internet Explorer submits a form or otherwise passes information to a Web server using CGI, the Web server receives the information from the client and passes it to the gateway program for processing. The gateway program then returns the result of the processing to the server, which returns it to the Web browser as an HTML page.

CGI applications are often used as form handlers for Web forms, and are executed using a <FORM> tag embedded in the form document, as in the following:

<FORM METHOD=POST ACTION=
"http://www.northwind.microsoft.com/cgi-bin/results.pl">

In this example, the Perl script results.pl in the cgi-bin directory functions as the form handler for processing the information submitted using the form.

History of Common Gateway Interface

In 1993 the National Center for Supercomputing Applications (NCSA) team wrote the specification for calling command line executables on the www-talk mailing list.[2][3][4] The other Web server developers adopted it, and it has been a standard for Web servers ever since. A work group chaired by Ken Coar started in November 1997 to get the NCSA definition of CGI more formally defined.[5] This work resulted in RFC 3875, which specified CGI Version 1.1. Specifically mentioned in the RFC are the following contributors:

  • Rob McCool (author of the NCSA HTTPd Web Server)
  • John Franks (author of the GN Web Server)
  • Ari Luotonen (the developer of the CERN httpd Web Server)
  • Tony Sanders (author of the Plexus Web Server)
  • George Phillips (Web server maintainer at the University of British Columbia)
  • Historically CGI scripts were often written using the C language. RFC 3875 “The Common Gateway Interface (CGI)” partially defines CGI using C,[1] as in saying that environment variables “are accessed by the C library routine getenv() or variable environ”.

The name CGI comes from the early days of the web, where users wanted to connect databases to their web servers. The CGI was a program executed by the server that provided a common “gateway” between the web server and the database.

CGI Scripts and IIS

CGI was developed for UNIX-based systems and is supported by most Web servers, including Internet Information Services (IIS). Microsoft Internet Server API (ISAPI) is a set of server extensions for IIS that functions similar to those of CGI but uses fewer resources. The main difference is that with CGI the system creates a unique process for every CGI request, while ISAPI extensions do not require separate processes. This makes ISAPI applications generally more responsive than CGI applications.

CGI Script Example

The following Perl program shows all the environment variables passed by the Web server:

#!/usr/bin/perl

=head1 DESCRIPTION

printenv — a CGI program that just prints its environment

=cut
print "Content-type: text/plain\n\n";

for my $var ( sort keys %ENV ) {
 printf "%s = \"%s\"\n", $var, $ENV{$var};
}

CGI Gateway explained

https://youtu.be/9Zx1vEmaQMc
What is CGI?

Search