CGI 'Hello World' program

CGIs are one means of producing dynamic web pages. They can alter the content of your Web pages based on browser, time of day, user information or a host of other factors. For large-scale web application development, alternatives to CGIs such as FastCGI, ISAPI, Java Servlets and Zeus Distributed Authentication and Content Generation API exist and should be considered for performance reasons. However small, simple CGI programs can make your Web site a far more interesting place to be than purely static pages.

CGI is an acronym for Common Gateway Interface, it defines the method used by web servers to communicate with external programs. CGIs are generally easy and quick to produce and can be written to power even the most complicated of Web sites. It should be noted that the flexibility of CGI programs is also a potential weakness, not least the security concerns of badly written programs. This document is intended as an introduction to CGI programming, not a comprehensive tutorial. Users wishing to taking CGI programs further than the scope of this document should consult the wealth of online documentation or a good book on the subject.

CGI Programs can be written in almost any language, including but not limited to C/C++, FORTRAN, Pascal, Perl, SH and Python. CGI programs are often referred to as CGI scripts, although scripting languages are a popular way of developing CGI programs it is a misnomer. CGI programs can be, and often are traditional compiled programs. A particular advantage of scripting languages for CGI programming is portability. An interpreted language is not tied to one platform, and should easily transfer between different Operating Systems providing an interpreter exists. Whereas compiled languages will always have to be recompiled if you change your web server machine to another platform or OS.

We shall primarily use Perl (Practical Extraction and Reporting Language) for our examples, Perl although originally a UNIX tool, is now available on almost all computer platforms including Windows / NT and Apple Macs. Perl is an interpreted language and so may run a little slower than compiled programs. It does however have a number of useful functions for dealing with Strings and Lists which are applicable to CGI programming.
Basic Concepts

The beauty of CGI programs lie in their simplicity. In order to interface with the Zeus Server all you need to do is write to the standard output stream from your chosen language. In Perl this would use the print function, in C you would useprintf(). Any information generated in this way will be passed on by the Zeus server to the client. CGIs can output any type of information, regular text, HTML, images, even audio. It is therefore important for a CGI to identify what type of information it is sending back to the browser so it is correctly displayed. The browser expects any data it receives to be prefixed with HTTP Headers, specifically in this context, a Content-Type header. The Content-Type header needs to be set to the correct MIME type of the data sent to the browser. Generally your CGIs will return text, or HTML which have MIME types of text/plain and text/html respectively.

So an example of the first line your CGI program needs to output will be

print( "Content-Type: text/plain\n\n" );

The "\n\n" (a user visible blank line) is required to terminate the HTTP Headers section before you send back the data.

My First CGI Program

We now know enough information to write a real CGI program. We shall start where all good tutorials start, with a hello world program.

Hello World in Perl:

#!/usr/bin/perl
print "Content-Type: text/plain", "\n\n";
print "Hello World in Perl", "\n";

The first line of our Perl program tells the web server where to find the Perl interpreter, contact your local system administrator if you don't know where this is. We then output the HTTP Headers, in this case we only supply the Content-Type field, followed by the blank line required to terminate the Headers section. Finally we output our data.

Hello World in C:

#include <stdio.h>
int main(void)
{
  printf("Content-Type: text/plain \n\n");
  printf("Hello World in C! \n");
}

These short, simple programs show how easy it is to write CGI programs, they are not however particularly dynamic! To produce dynamic web pages the CGI programs require information from the server on which to act. There are two types of information which the Zeus Server passes to the CGI program.

  • Information about the client (browser), server (Zeus) and Authentication User.
  • Data submitted via HTML forms.

All this information, with the exception of some types of form data, is passed in environment variables for the CGI program to read.

Content Manager [Administrator] 16 December 2005  Permalink  
Download Free Trial

Recent Articles

Other Resources



www.zeus.com