Slide 3.7: Writing HTML scripts
Slide 3.9: Writing CGI scripts
Home

CGI (Common Gateway Interface)


Normally clicking on a link in a web browser causes the web server to return a static HTML page. No matter who clicks on this link or how many times they do it, the resulting returned web page is always the same. To change a static HTML page, the site's webmaster must edit the contents of the HTML file. On the other hand, a CGI script allows a link or a button in a web page to run a program on the web server.
CGI is a standard for running external programs from a World-Wide Web HTTP server.
This CGI program can do any number of things from getting the current date and time to performing a complex lookup and update in a database. In either case, the results are not the same everytime the link or button is pressed. Commonly, the program will generate some HTML pages which will be passed back to the browser. CGI allows the returned HTML page to depend in any arbitrary way on the request. The process occurs something like this:
  1. User clicks on a link in a web page (e.g. http://www.cgitest.com/cgi-bin/test.cgi).
  2. The web server runs the program test.cgi.
  3. The test.cgi program does what it is programmed to do.
  4. The test.cgi program also builds an HTML file in memory and sends it back to the user's browser.
The program can perform whatever operations it needs to and it can then generate an HTML page based on the results of these operations. When the CGI script is used with a database such as MySQL, many things are possible. Generally, the page returned to the user's browser contains the results of the database search. Or, if the user had provided information through a form in the web page, the database records were updated. A CGI program can be any program which can accept command line arguments. Perl is a common choice for writing CGI scripts.