Common Gateway Interface

The Common Gateway Interface (CGI) is a standard for interfacing external applications with information servers, such as HTTP or Web servers. A plain HTML document that the Web daemon retrieves is static, which means it exists in a constant state: a text file that doesn't change. A CGI program, on the other hand, is executed in real-time, so that it can output dynamic information.

For example, let's say that you wanted to "hook up" your Unix database to the World Wide Web, to allow people from all over the world to query it. Basically, you need to create a CGI program that the Web daemon will execute to transmit information to the database engine, and receive the results back again and display them to the client. This is an example of a gateway, and this is where CGI, currently version 1.1, got its origins.

The database example is a simple idea, but most of the time rather difficult to implement. There really is no limit as to what you can hook up to the Web. The only thing you need to remember is that whatever your CGI program does, it should not take too long to process. Otherwise, the user will just be staring at their browser waiting for something to happen.

	- from <http://hoohoo.ncsa.uiuc.edu/cgi/intro.html>

Here is the CGI page from W3 - http://www.w3.org/CGI/
Resources


CommonGatewayInterface QuickQuestions

Q: With WindowsXp, if I have CGI directory defined, what do I use for setting file permissions (instead of CHMOD) so unauthorised users are barred?

A:

Q: Is there still a need to use CommonGatewayInterface in a shop with MicrosoftWindowsCulturalAssumption? With ActiveServerPages (ASP) on InternetInformationServer (IIS), what advantages has CommonGatewayInterface offer? Is there not a great concern over CgiScriptSecurity that drive people to using its alternatives? -- DavidLiu

A:

From IwannaLearnPerl:

Q: IwannaLearnWebSiteProgramming and get bumped here via a contribution to that page. After looking at the reference provided at http://users.easystreet.com/ovid/cgi_course/lesson_three/lesson_three.ht I got further confused. I have heard of CGI scripts before, never programmed in it, and I know WardsWiki is based on Perl scripts. My company used ASP (and DotNet) on IIS and therefore I presume they do not use CGI nor Perl. I am confused by how are PerlLanguage and CGI related. Anyone to help untangle a website ignorant beginner out of the Perl vs (or and?) CGI confusion? Thanks from DavidLiu and DeleteWhenCooked

A: CGI is not a programming language; it is a standard for how data from a web form is formatted, and for how the program interpreting that data should format its output (so that the web browser knows what type of data is being sent). Perl became synonymous with CGI because in earlier years it was pretty much the only sane (?) choice for scripting; parsing the input in a lower-level language is rather difficult, plus it is hard to arrange for your code to run on someone else's web server unless you have access to their compiler, or at least one for their platform. (Not to mention the security risks associated with opening up possible BufferOverflows? to exploitation by anyone on the InterNet.) At that point other scripting languages (Python, PHP etc.) either had didn't exist or were not well known, and plain shell scripting usually isn't enough to do the job.

Since Perl has considerable momentum in this field, it comes as no surprise that a "CGI.pm" module is available by CPAN, which takes care of most of the minor details. But doing it yourself is not too difficult; all you need to know is

  1. GET data (which follows a ? in a URL) arrives via $ENV{'QUERY_STRING'}; POST data (submitted via a form) arrives via the standard input.
  2. Input is formatted as an ampersand-separated list of "key=value" pairs; the '+' character represents space, and '%xx' (two hex characters) is a character escape sequence.
  3. Output should start with "Content-type: text/html" (or whatever is the MIME type of the data you want to output; of course HTML is the common case) and two newlines, and should go to standard output.

-- KarlKnechtel
For server side CGI scripting languages take a look at CategoryPhp, CategoryPerl, CategoryPython, and CategoryRuby.
See: FastCgi

CategoryInterface, CategoryInternet, CategoryWebDesign

EditText of this page (last edited March 3, 2011) or FindPage with title or text search