< Index - Next >
What is CGI?
- Common Gateway Interface -
CGI is a lightweight mechanism for communication between the web server and other programs (CGI scripts) that can be run on the server.
Plenty of web resources to guide both learner and developer
cgi-bin
Instead of pointing to an HTML file a URL may request that a CGI program is run on the server.
CGI programs (or scripts) are commonly written in:
- Unix - Shell, Perl, Tcl, C,C++, Python, Ruby
- Windoze - Visual Basic, Perl
- Mac - Applescript, C,C++, Perl
CGI scripts are usually kept in a special directory, named cgi-bin, to provide security and ease of server management.
System security is ensured by restricting access to cgi-bin and/or restricting the script languages that the server may execute.
Handled through the server configuration.
e.g. Apache httpd.conf ScriptAlias directive identifies which directory is available to run cgi scripts:
ScriptAlias /cgi-bin/ "/home/gonzo/public_html/cgi-bin/"
Visibility of the cgi-bin can be controlled.
The following only allows the cgi scripts to execute within the domain gre.ac.uk
<Directory "/home/gonzo/public_html/cgi-bin">
AllowOverride None
Options None
Order deny,allow
Deny from all
Allow from .gre.ac.uk
</Directory>
Although CGI scripts do not need to be in cgi-bin...
AddHandler cgi-script .pl
... allows perl files with the extension .pl execute outside cgi-bin.
The cgi files need to be made globally executable
-> chmod 755 somefile.pl
-> ls -l somefile.pl
-rwxr-xr-x 1 gonzo users 645 Oct 28 10:58 somefile.pl
CGI scripts can be used to perform a diversity of functions such as:
- Searching
- Imagemap handling
- Database access
- Form handling
Alternatives to CGI include Microsoft's Active Server Pages (ASP.NET) and the increasingly popular PHP.
CGI remains widely used because:
- it is well understood technology
- it is essentially browser independant
- it is relatively standard across servers
- CGI programs can be written in a variety of languages
- some languages, notably Perl, offer remarkable power
CGI Sequence of actions

- The client browser sends a request (HTTP message) to the server
- may be triggered in several ways - e.g. form submit
- names the URL of the CGI program
- data can be sent with the message - get/post
- Server software on the server machine receives the message
- Server calls the CGI program
- information passed to the CGI program
- get/post data
- cookies
- client environment data
- server environment data
- CGI program runs
- has access to server resources - e.g. filesystems, databases
- outputs a response - e.g. an HTML document
- Server software routes stdout from the CGI program back to the client
- Client browser receives the HTTP response
Simple "Hello" Example
A Perl CGI program "hello.pl" can be run by clicking this anchor
Forms (oh no! not again)
The HTML <form> tag has three attributes that control the interaction of the form with the CGI program on the server:
- action provides the URL of the program that is to process the form data.
- method specifies how the name=value pairs are transmitted
- get - data is transferred as part of the URL
- post - data is transferred after the URL in the same http data stream
- enctype specifies the data encoding used in transmission
- default is Internet Media Type (application/x-www-form-urlencoded)
<form action="../../cgi-bin/form.pl" method="get"
enctype="application/x-www-form-urlencoded">
< Index - Next >
© Kevin McManus - 2000, 2001, 2002, 2003, 2004