CGI program

<World-Wide Web> (Often "CGI script") A program running on a web server to produce dynamic content, usually an HTML web page, in response to a user's request.

The Common Gateway Interface specification defines the interface between the web server and such programs.

The program can access any data that a normal application program can, however the facilities available to CGI programs are usually limited for security.

Although CGI programs can be compiled programs, they are more often written in a (semi) interpreted language such as Perl, or as Unix shell scripts, hence the name "CGI script".

Here is a trivial CGI script written in Perl.

(It requires the "CGI" module available from CPAN).

#!/usr/bin/perl use CGI qw(:standard);

print header, start_html, h1("CGI Test"), "Your IP address is: ", remote_host(), end_html;

When run it produces an HTTP header and then a simple HTML page containing the IP address or hostname of the machine that generated the initial request.

If run from a command prompt it outputs:

Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <HTML><HEAD><TITLE>Untitled Document</TITLE> </HEAD><BODY><H1>CGI Test</H1>Your IP address is: localhost</BODY></HTML>

The CGI program might be saved as the file "test.cgi" (or in the appropriate directory on a web server, e.g. "/home/httpd/cgi-bin/".

A user could then type the appropriate URL, e.g., into their web browser to get the program to run and a custom page produced for them.

Early web servers required all CGI programs to be installed in one directory called cgi-bin but it is much better to keep them with the HTML files to which they relate unless they are truly global to the site.

All modern web servers make this easy to do.

< Previous Terms Terms Containing CGI program Next Terms >
CGI program
CGI script
Common Gateway Interface
CGI script