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 scripts can be used to perform a diversity of functions such as:
Alternatives to CGI include Microsoft's Active Server Pages (ASP.NET) and the popular PHP.
CGI remains widely used because:
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:
CGI scripts are usually kept in a special directory on the server, named cgi-bin.
This provides 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
The HTML <form> tag has three attributes that control the interaction of the form with the program on the server:
For XHTML validity the <form> tag must be outside any container but the various form elements must be inside a container such as a paragraph or table.
<form action="/~mk05/cgi-bin/form.pl" method="get" enctype="application/x-www-form-urlencoded"> <p> Form components </p> </form>
This form demonstrator is a simple static HTML page that demonstrates several important concepts in the use of forms and the development of stateful web applications. Information is transmitted from this page to server side scripts in several ways. When the POST form to info.php button is clicked information is passed to the server side script info.php as GET, POST and cookie data.
Cookies are an important mechanism for implementing stateful web applications. This page demonstrates other ways of storing state information. State may be stored visibly as default values in form text fields and as default selects for checkboxes, radio buttons and options. State may be concealed in hidden form fields. And what about those GET parameters? This means of storing state is often referred to as URL re-writing. If this page were dynamically generated by a CGI program or server side script then all of these provide possible storage mechanisms for state.
This page demonstrates a bug in the W3C validator as it incorrectly identifies the & symbol in the JavaScript GET strings as being an XML entity.