JDT

 

FREE Downloads
Articles
Tutorials

 

Common Gateway Interface (CGI) - Part 2


CGI programs provide one way to create dynamic content. The server can recognise when a URL request requires a CGI program to be run in order to generate content for a web page. The program generates the content, and the server passes it back to the client (the web browser).

Everytime a URL request is made that calls the CGI program, a new CGI instance is started (by the server).

Languages

CGI programs are often written in Perl, but they can be written in C, Pascal, Lisp, or many other languages. CGI itself is not a language, but is an interface between a web server and the program being run.

Output

Although CGI programs often generate HTML content, they can generate other content such as images or video.

A simple CGI program (written in Perl)

1: #!/usr/bin/perl -w
2: use CGI qw(:standard);
3: use strict;
4: print header;
5: print "Goodbye cruel world";

Line 1 - This defines the location of the Perl interpreter on the web server. It must be the first line in the program. The -w enables warnings to be generated if the program contains any uninitialized variables.

Line 2 - Include the CGI module in the program. qw(:standard) causes the standard set of functions from the CGI module to be imported into the program.

Line 3 - Use strict enforces good coding practices. If 'use strict' wasn't used, certain coding errors would be allowed to slip through when the program was run.

Line 4 - Print header. This imports the header function from the CGI module. It prints a standard header (defining the content type) so that the browser knows what is being sent to it. A header will typically look like:

       print "Content-type: text/html\n\n";

Line 5 - Displays a line of text on the screen.

Running the CGI program

To run the program it needs to be copied into the correct directory (typically, cgi-bin) and needs to be made executable (chmod 755). You can then browse to the program to run it.



Go back to Articles home page

Go back to Programming and Web Development Articles home page


A comprehensive selection of FREE Open Source Software Downloads is available from this site.

New software is added on a regular basis, with the software either being available for download directly from this site, or a link is provided to a third party site from where the software can be downloaded.

 


JDT

Copyright Notice for John Dixon Technology Ltd

Privacy Statement

Terms & Conditions