CGI stands for Common Gateway Interface, a standard way for browsers to interact with programs running on the web server. It enables content to be generated dynamically, as opposed to static HTML pages.
Contents
Overview
Environment
Forms
Links
Normally with web pages, you write the page as an HTML document, put it on the server, and people go and view it. Sometimes, though, you want the information on the page to be dynamic: perhaps a table of values generated from a database query, or up-to-date share price information; and sometimes you want to give the user more control over what is displayed, or retrieve data from them. All these are possible with CGI.
CGI programs are programs that are run on the web server. Normally, the browser asks for an html document, the server finds it and returns it. With CGI, the browser asks for a CGI document (usually determined by the fact is is in a cgi-bin directory, or its file extension is cgi), the server runs the CGI program and sends its output to the browser. Obviously the CGI program has to output HTML data for the browser to understand it, and it must also send the following line at the start, followed by a blank line:
Content-type: text/html
A CGI program can be written in any language, as long as it can run on the web server. Perl is common, and other scripting languages are valid, as are compiled languages such as C. The program needs to be runnable by the www user.
CGI scripts wouldn't be very much use if you couldn't pass parameters to them. This is done at the client end by adding a question mark and some text at the end of the URL. For example, calling a maketea script with parameter milk,twosugars would look like: <A HREF="maketea.cgi?milk,twosugar>. At the server side, the cgi script sees this parameter as the environment variable QUERY_STRING. The two links below call a script with different parameters. The script code is here
Note: these scripts don't work as my web host doesn't allow CGI
There's another environment variable that can be very useful. If a directory is password protected using the htaccess mechanism, the environment variable REMOTE_USER is set up to the login name. Here is an example script.
We can now think about adding buttons, lists, edit boxes and so on, to build up a form the user can make choices on. These all go in a FORM element, and each can have a name and a value. These names and values are passed on to the script in a string of form "name=value&name=value...". Some of the form elements are:
Here is an example form. If my server allowed CGI scripts, you would see a result. Unfortunately, it doesn't.
Parent Document: Technical Investigations
Author: David Meiklejohn
Web Page: here
Last Updated: 23/11/1999
Review Date: 01/10/2000