Introduction to the Common Gateway Interface

This document was written by CS 290W TA David Corcoran and was last modified

What is CGI?

The Common Gateway Interface (CGI) is what allows a Web server to run APPLICATIONS that communicate with Web pages. Remember that Java and JavaScript APPLETS are run on the client. They are downloaded to the Web browser before execution.

CGI is run on the server which makes it especially useful for applications that need access to files and facilities available on the server (like databases). CGI applications do not monitor events on the client. You cannot perform image swapping on the browser using OnMouseOver events. Refer to the following diagram:

********* ++++++++++ * Web * ---> + Web + ---> +++++++++++++++ *Browser* + Server + CGI APPLICATION * * <--- + + <--- +++++++++++++++ ********* ++++++++++ Here is actually what is going on:
  1. The Web Browser makes a request to the Web server for the CGI application (<FORM ACTION="cgi-bin/guest.cgi" METHOD=POST>)
  2. The Web Server executes the corresponding CGI application and sends it the POSTed data from the Web page if any exists.
  3. The CGI application executes returning data back to the Web Server.
  4. The Web Server passes this data back to the Web Browser.

CGI programs can be written in virtually any programming language. The following is a list of a few languages capable of CGI.

One of CGI's most common uses is in processing forms. Take the following form for example:

<FORM ACTION="cgi-bin/guest.cgi" METHOD=POST>
<INPUT TYPE=TEXT NAME=lookfor SIZE=20 VALUE="John">
<INPUT TYPE=SUBMIT VALUE="Search Database">
</FORM>

This form takes a text value which is default "John". When the user presses the Search Database button, the Web browser will call the Web server at http://icdweb.cc.purdue.edu/ and say "Hey, I need you to execute guest.cgi in directory ~bxd/cgi-bin" and also "I'm going to send you some information to send to that CGI script." In METHOD=POST that information is sent as a data file.

In METHOD=GET that information is known as the QUERY_STRING. Basically the QUERY_STRING is a string of characters appended to the end of the URL that it is calling. The previous might actually call:

http://icdweb.cc.purdue.edu/~bxd/cgi-bin/guest.cgi?lookfor=John With correct interpretation the CGI script can remove the postpended characters and locate information pertaining to lookfor=John.

What is the deal with this Gateway business? The reason it is called Common Gateway is because CGI scripts act as gateways to other programs running on the Web server. Say there is a database running on the machine. I could feasibly do a query on that database using the information passed in by lookfor and present that data back at a Web page to the Client.

For more information on CGI including some commonly used scripts please visit http://hoohoo.ncsa.uiuc.edu/cgi/overview.html

[ Back to Main ]