NSW Schools

Printer Version of Page


Server Scripts

  • Common Gateway Interface (CGI)
  • Perl CGI Scripts
    • Sample Scripts
      • Hello My Name Is
      • The Mangler
    • Perl Script Syntax
  • Uploading & Executing Scripts

Common Gateway Interface (CGI)

CGI stands for Common Gateway Interface. To permit interaction between a server and a browser servers support executable scripts. CGI stands for Common Gateway Interface. CGI provides a standard for interfacing external applications with information servers, such as HTTP or Web servers. A CGI program, is resident on the webserver as an executable script.

  • The scripts cause the server to execute a program and return appropriate output when a browser requests a designated URL. In this sence HTML pages generated by scripts are said to be dynamic pages - rather than static HTML pages whose content is fixed
  • GCI scripts are frequently used for installing "Hit Counters", Image Maps and Database Access Facilities on Internet Sites. They were also used almost universally for processing user input forms.
  • In order to set up a CGI script it must be installed on a webserver, frequently in a folder called "cgi-bin". The configuration of the script is achieved by setting the values of certain required variables.
  • Webservers that allow script execution associate the file extension .cgi with executable cgi programs. Webservers may also be configured to execute all files under certain subdirectories as executable scripts

While the cgi standard ensures that all cgi scripts can be interpreted by all cgi compliant servers a script designed to run on one webserver that calls another program (such as an emailer or image map interpreter) will likely crash if installed on a webserver that does not support the component program being called by the cgi script. In this sence cgi scripts are platform dependent.

cgi scripts can be written in any of a number of both compiled and non-compiled languages that can be executed by the webserver on which the script is resident. Relevant languages include, C, Fortran, Visual Basic & AppleScript . If you use a programming language like C or Fortran, you know must compile the program before it will run. The distinction between compiled and non compiled programs is that (Source:

"An interpreted program, sometimes called a script, is a program whose instructions are actually a logically sequenced series of operating system commands, handled one at a time by a command interpreter. In turn, the command interpreter requests services from the operating system. The writer of the interpreted program need not be concerned by low-level storage management considerations. On the other hand, an interpreted program can not be as efficient as a compiled program, which has been processed by a language compiler. A language compiler converts source statements into something close to the strings of 0's and 1's that a processor ultimately is given to work on. Because this work is already done before a compiled program is run, it runs much more quickly.

An interpreted program in a language such as Perl is much easier to write than a program written for a compiler. Interpreted programs are ideal for small tasks and for "gluing together" a succession of compiled programs that are run from the interpreted program."

Perl CGI Scripts

  • Sample Scripts
  • Script Syntax
  • Uploading & Executing Scripts

Sample Scripts

  • Hello My Name Is
  • The Mangler

Hello My Name Is

See the Hello My Name is CGI/PERL Script Run:

The code for this script is:
# prints a users name using a GET request!
print "Content-type: text/html\n\n";
print "Hello, world My Name is ";
print "$name";

The Mangler

Dr Lincoln Stein has installed "The Mangler" on the server of the Cold Spring Harbour Laboratory. The Mangler uses the Travesty algorithm to rearrange webpages.

Dr Stein has published the source code for this script:

Perl Script Syntax

  • The first line of the perl script #!/usr/bin/perl tells the Unix shell on the server where to look for the perl interpreter. It must be on the first line of all perl scripts
  • Subsequent lines begining with a hash (#) sign are comments and are ignored by the perl interpreter
  • All cgi scripts resident on a Webserver need to print a content type
    • "Content-type: text/html\n\n"; tells the script that it is to print text/html

Uploading & Executing Scripts

  • Scripts must be upload in ASCII mode. In Binary format, invisible characters which will cause the script to crash may be inserted in the script itself.
  • Scripts must be written in editors with Word Wrap switched off
  • Scripts and the directories in which they reside must have the correct file permissions.
  • Directories containing scripts should be executable by the public which corresponds to Octal Value 711
  • Scripts must be readeable and executable by the public which corresponds to Octal Value 755 on Linux/Unix systems

Further information on using Perl to write cgi scripts:

Inline Image - Go To