Common Gateway Interface
The Common Gateway Interface (CGI) defines how information is passed from the client's browser to the webserver. Server side scripts may be called from the client with a HTML file reference to the server side script in any of the following ways:
- An HTML Anchor tag reference. This is normally used as a link to a program which will generate an HTML page. It can be used to display something like date and time or any data on the server.
- A SSI (Server side include) tag reference. This may also be used similar to anchor tag references, but is normally used to display the date the HTML file was last referenced. Also this may be used to generate hit counts.
- Using the HTML FORM tag with the action attribute set to the location and name of the server side script. This way the to get the user's data entered in the form is invoked. This is normally used to send user entered form data from the client to the server. This is the main focus of this section.
- The client script may directly reference the server side script.
This means that the HTML file that is sent to and displayed on the user's web browser contains the server side script program reference. Therefore the following two items are required to get form data from the user to the server:
- An HTML FORM with the action attribute set to the location and name of the server side script. A client side script may also support the FORM by making sure entries are correct when the "Submit" button is clicked. If a client script is used it may be written in JAVA or VB (Visual Basic) Script.
- A server side script, normally residing in a "cgi-bin" directory. The required location of the server side script is dependant on the web server configuration.
The client script may be JAVA or VB (Visual Basic) Script. Server side scripts may be written in almost any language that produce interpreted or executable code so long as the webserver is configured to support it. This typically includes C, Perl, Python, shell script and others.
Data Access Methods
The two methods used to access the data are:
- POST - Sends data to the standard input. The variable CONTENT_LENGTH must be used to tell how much data to read.
- GET - Sends data to the environment variable QUERY_STRING. The length of this string is limited so it is better to use POST when large amounts of data are expected.
If all this is hard to grasp, don't worry, there will be many examples. There are many environment variables used by the HTTP to CGI interface which provide additional information about the HTTP transfer.
Server side script response
When the CGI program produces output to its standard output stream, the web server program, such as Apache or Microsoft's Internet Information Server will send the data from the script program to the client's web browser. The requirement for this to happen is that the server side script program first output a Content-type header followed by a new line character. An example of the header is:
In Perl this may be done with:
print "Content-type: text/html","\n";
When this header is output by the server side script program the web server will provide the rest of the required HTTP headers. However all the headers may be provided by the server script program rather than using the above header. For example, a script program may be written to output a standard HTML file.