CGI Scripting
Perl CGI Index
Basic CGI Scripting
Simple CGI Demo
Styling CGI Output
Sharing Perl Code
CGI Code Suite
CGI Environment
Next section...
Web Applications

CGI Scripting

Basic CGI Scripting

By default the Apache webserver assumes all file resources are text/html and serves them directly.

To designate certain resources as executable scripts they are placed within a special directory usually named /cgi-bin.

This is defined within the Apache configuration as 'ScriptAlias'

ScriptAlias /cgi-bin/ "c:/mywebsite/cgi-bin/"

In essence this will match all requests for /cgi-bin/ in the request URL to the specified directory on the server.

Apache will treat every file within that directory as executable, look up its shebang line and pass it to the required interpreter.

In addition to this the Apache configuration allows some resources in locations other than the /cgi-bin/ to be identified as executable by their file extensions, usually .pl .cgi .vb or similar. These are set by the Apache AddHandler option.

AddHandler cgi-script .cgi .pl

To enable CGI programs at all the ExecCGI needs to be set as an option within the Apache directory configuration, in addition Unix based servers will also require execute permissions to be set for CGI script files, this has no relevance on a win32 system.

CGI scripts have two key syntaxes that must be correct for it to work. The first is the 'shebang' line. The actual path varies according to the server setup but is usually something like:

#!/usr/local/bin/perl

This has to be the very first line of the file, on execution this line will direct control to the specified interpreter, in this case Perl.

Note! On a win32 system the Unix based path will not have any meaning, instead the path to the Perl interpreter must be added to the DOS environment variable 'PATH' and the correct shebang line for the script is then #!perl

If you are developing on a win32 platform but have your live host server running Unix then you will need to ensure that this shebang line is modified on upload. For more on this see the 'Home Webserver Notes' pages...

The second critical feature of any CGI program is that it must output a specific line as the very first line of output, and that must be followed by an empty line before the regular output from the rest of the page.

#!/usr/local/bin/perl
print "Content-type: text/html\n\n";
print<<HERE;
<html><head><title>Hello World Example CGI Script</title>
</head><body><p>Hello World!</p>
</body></html>
HERE
# EOF

In practice the program output is returned to the HTTP stream with its content-type header set as for a regular webpage and the rest of the file from <html> to </html> is ordinary HTML and handled as such by the browser.

CGI programs can take input as well. Arguments can be either added to the URL or passed via a webform. Within the program these values can be retrieved by directly querying the CGI environment values or more easily by using a simple function.

URL arguments take the form:
http://www.website.com/cgi-bin/myprog.pl?arg1=val1&arg2=val2&arg3=val3

The ampersand is however a reserved character in the xHTML specification so instead we more usually use semi-colons as argument or parameter separators:
http://www.website.com/cgi-bin/myprog.pl?arg1=val1;arg2=val2;arg3=val3

Within a webpage form, any form input element is automatically passed as a parameter to the CGI environment and is identified by its 'name' attribute.

Show Style-Switcher...