Using CGI

The Common Gateway Interface is one of the oldest ways of adding interactive features to your web site. CGI is done by using programs written in one of a variety of different languages and can be used for example to process the input from one web page to dynamically generate the content of another page. Other more modern and fancier server side scripting languages are often implemented behind the scenes using CGI.

PERL is probably the most common language used to write CGI scripts but it isn't necessary to learn this or any of the other programming languages that can be used to write CGI programs because there are a number of web sites that make a lot of these programs freely available for you to use.

One commonly available program is the form2mail script. This program will take the input from a form and create a nicely formatted email that it will send to a specified email address. The script can then display another web page (usually referred to as the thankyou page) once the email has been sent. Without using such a script the email generated from a form will be difficult to read and the visitor will have no indication that the email has been sent, also if they are using a really old browser then their email program will be opened instead of the form generated email being sent.

You have two choices when it comes to CGI scripts if you don't want to write your own. You can either use one that is already set up on a CGI server (with the owner's permission of course) or you can load the program to the CGI-BIN associated with your own web site. You'll find some sites where you can obtain CGI programs in either of these two ways on my webmaster links page.

To use a CGI script that is already set up on the server you just need to follow the instructions on that web site to sign up and gain access and include the script into your page source as specified by that script service. This is the easiest way to get CGI programs to use with your site but the degree of customization that can be done to blend the services into your site may be limited particularly with free services that display advertising. Sites like this often also provide a paid version of their services that removes the ads and may give you more access to customize the programs.

If you want full access to customize the programs to meet your own requirements then you will need to download the program source to your own computer, modify it to suite your needs, and then upload it to your own server. There are a number of sites that provide scripts that you can use in this way and the amount of modification that you do is entirely up to you. In most cases you can use the program as supplied with very little work being required on your part. Of course your web host must provide you with access to upload your own CGI programs if you are going to be able to use this option, most free web hosts don't allow cgi programs to be uploaded so you are limited to using either the ones that they provide or those from another hosting service that allows remote access.

To use a script on your own server requires a bit more work than using one that is remotely hosted or is already configured on your host server. If for example the script uses PERL, you will need to first check that your server has PERL installed (ask your web host if you're not sure). You also need to know the path to the CGI-BIN on your server or alternatively the file suffix that your CGI programs need to use. You will probably also need to modify the first line of the supplied script to specify the absolute path on the server to where PERL can be found. Finally you may need to telnet into the server and set the file permissions so that the script can run.

The first line of a PERL script should look something like this:


where usr/bin/perl is the path on the server to the PERL executable. To set execute permissions for your program you will probably use the chmod a+rx command or alternatively chmod 755, in each case specifying the name of your program on the end of the command.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow