homeCGI for non-programmers

If you require interactivity on your Web site, but are daunted by the prospects of CGI and other programming languages, stand by because all you need to know to utilise CGI is here in a (wal)nutshell.

CGI stands for Common Gateway Interface and is used to process information on the Web server itself. The most common examples would be hit counters, email forms and guest books. In all these cases it is necessary for information to be retrieved from the browser, and processed somewhere central, and where better than your Web server? Just taking the hit counter as an example:

A user visits your Web page. The page has been requested from the server by a browser, therefore the server needs to register this fact somewhere to total hits. It actually does this by adding 1 to the number contained within a file containing only the number of visitors to your page. This information needs to be added to the page for the next visitor, so that there is now a two-way conversation between the server-side counter log and the client-side browser. The conversation is conducted through a CGI script, and in a language such as Perl (the most commonly utilised programming language for CGI - and the one I will refer to from now on).

So, hopefully the concept is clear, but what about the actuality? Programming languages can be daunting, but with CGI there is a wealth of ready-made scripts available on the Web (my favorite site for this is CGI Resources). So, all we need to know is how to use those scripts.

In most cases, the authors of our ready-made Perl scripts have included instructions on what needs changing within the script itself. However, there are one or two absolute necessities to make this work:

The Perl interpreter:

Perl, unlike some other programming languages, is not compiled - it can be viewed in its native format in any text editor just like HTML. Therefore, it needs a Perl interpreter on the Web server to operate. The first line of code in any Perl script states the location of the Perl interpreter like so:

#!/usr/bin/perl

You should ask your host company what the path to the Perl interpreter is, or, if they have provided you with some sample scripts, download one and look at the first line of code. In my experience, it is quite often the above.

NOTE: Whatever you do, do not leave out the "#!" At the beginning, and start the path with a "/" as above. In addition, it is important that you do not remove any carriage returns from within Perl code as this will often stop the script from functioning.

Permissions:

Each CGI script will require configuration for its permissions. This is often referred to as CHMOD - the UNIX command that sets file permissions. Just about every ready-made script will tell you in advance what the permissions should be set to, but they may describe it in different ways. Sometimes it will be in numerical terms, such as "CHMOD 755", sometimes in shorthand such as "rwxr-xr-x" and sometimes in plain English such as "The owner has rights to read, write and execute. The group and world to read and execute". Now, it's not within the remit of this article to explain CHMOD in detail, or the complexities of the UNIX operating system, so I will merely point out that the Owner is the first digit of a numerical system and the first three characters of an alphanumeric system. The Group is the second numerical digit and alphanumeric characters 4-6, and the World is the third numerical digit and alphanumeric characters 7-9. (see table below)

OWNER755rwxr-xr-x
GROUP755rwxr-xr-x
WORLD755rwxr-xr-x

So, regardless of which method is described to you, or which method you use, you can logically understand each. As to what they mean? Well, r=read, w=write and x=execute. Numerically, each number is calculated by adding all the values together, with each attribute being represented thus:

OWNERGROUPWORLD
rwxrwxrwx
421421421

So, 755 means that the owner has read, write and execute rights (4+2+1), whilst both the Group and World have read and execute rights (4+1). Therefore 755 is the same as rwxr-xr-x.

Got it? Good : )

If you are unsure of how to set these permissions and which software to use, I'd recommend using WS_FTP and following the instruction sheet here.

So, we have looked at what needs changing for the script itself, what about the HTML? In most cases this will be given to you as part of the script download (unless the HTML is embedded within the script, in which case you access the script directly through the browser) but generally it is much the same. Lets look at a standard use - a feedback form (or indeed any form using CGI such as a guest book).

I will assume you know enough HTML to understand a FORM feature. FORM is a closed tag in that it is started by a <FORM> tag and ended by a </FORM> tag. It is within the <FORM> tag that we set our parameters, including where to send the information. The relevant FORM attributes here are METHOD and ACTION:

<FORM METHOD="how to handle the script" ACTION="where to send it">

The ACTION attribute determines where the information should be sent, which for us is our CGI script. So, the ACTION="/path/to/cgi_script.cgi". It is imperative that this path is correct. If in doubt use the absolute path rather than the local. Equally, if your CGI script refers to a document on your site, that path must be correct and again it may prove easier to use the full URL.

Testing the water

What better way to test the methods than to try getting a basic script to run. The link below will allow you to download a very basic Perl script generically referred to as a "Hello world" script (because all it does is display a few words in the browser.

Download the script

All you need to do is make sure the path to the Perl interpreter is correct, and set the file permissions to 755 and you should be able to see my message by directly viewing the Perl file in a browser (the path should be something like http://www.yourdomain.com/cgi-bin/test.cgi



John Lyons BA(Hons). egovision design and SouthBank University, London.

   
egovision


Articles and essays
As part of our on-going commitment to education and intellectual rigour, egovision are proud to make available a collection of written articles loosely related to the Web and the Internet in general.
Some of these essays will be the product of our own well worn keyboards, others are pieces made available to us, or submitted by those of you who share our interests.

If you have an article you feel would suit this resource, or even have an idea for an article please provide details via our response form and we will contact you as soon as possible.