CGI - Common Gateway Interface

A simple CGI script

CGI scripts do not run on any webserver. In SLIS, CGI scripts run on "ella". The following instructions are correct for ella. Other servers may be set up slightly differently. On ella you should create two directories: "www" and a subdirectory of "www" called "cgi". Both directories should be world readable and executable. All html files should be in the www directory, all cgi files in the cgi directory. The URL for the html files is The URL for cgi files is
(Instead of putting the cgi files into the cgi directory they can also be located somewhere else. But then they must be given a ".cgi" extension.)


1) Save some simple html code such as

<TITLE> Hello World</TITLE>
as a file in your www directory on ella. Look at it through a browser.

2) Save some simple cgi code such as

#!/usr/bin/perl -Tw
use CGI qw(:standard);
print header();
print "<HTML>
<TITLE>Hello World</TITLE>
in your cgi directory. Run "perl -T" on it to check the syntax. Change file permissions to executeable ("chmod 700 filename" or "chmod u+x filename"). Look at it through your browser.

It is quite possible that you now see a page "Internal Server Error". To find the problem: on ella you can run a script called "checkcgi" that might give you hints. Otherwise go through the steps listed below.

Notes: 1) "print header()" is the same as print "Content-type: text/html\n\n"; but it leads to fewer mistakes.
2) The -T switch in the commandline (#!/usr/bin/perl -Tw) is for security purposes.

Tips for dealing with an "Internal Server error":

1) The first line of the script must contain the perl command (#!/usr/bin/perl)
2) The line "use CGI qw(:standard);" must be somewhere at the beginning.
3) The very first print statement of your script must be "print header();"
4) The file permissions must be set to executable (chmod u+x filename).
5) The script should be executed by typing "perl -T filename" on the command line to check the syntax.

Processing forms with CGI

Normal HTML:

user requests
html document
server finds HTML file
and sends page back


user requests
a form
server finds the HTML form
and sends it back to user
user fills out form
CGI application executes
program and sends results
back to user


A sample form looks like this:

<form action="" method="post">

<input type="radio" name="drink" value="tea" checked > Tea <br>
<input type="radio" name="drink" value="coffee" > Coffee <br>
<input type="radio" name="drink" value="hot chocolate" > Hot Chocolate <p>

<input type="submit" value="Place order">

3) Include the form in an html document on ella (don't forget to change the URL of the form action so that it points to your cgi directory).

This is the source code of a cgi file that processes the form:

#!/usr/bin/perl -Tw
use CGI qw(:standard);

print header();
print "<html>
<title> Tea is served</title>
<hr><h1> Tea</h1><hr>

my $drink = param("drink");

if ($drink eq "tea") {print "You requested tea.";}
elsif ($drink eq "coffee"){print "You requested coffee.";}
elsif ($drink eq "hot chocolate"){print "You requested hot chocolate.";}
else {print "An error occured.";}
print "
Thank you for your visit. Please come again.

4) Save the cgi file. Run Perl on it to check for syntax errors. It will ask you to input parameters manually. Type "drink=tea" then "enter" and then hit Control-D. Then check to see if it works via the browser.

5) Add a checkbox to the form (such as "Do you want milk? Yes/No") and a text area where customers can type in what kind of cake they would like to order. Change your cgi script so that it includes these in its reply, such as "you requested tea with milk", "sorry we are out of chocolate cake". The checkbox and text area must have distinct names in the form. Their parameters are retrieved in the cgi file by using the "param()" function.

CGI without forms

The parameters that are passed to a CGI file do not need to come from a form. They can also be added to the URL in the following manner:
<A HREF="">
Several parameters can be passed to a CGI file. They are separated by a & symbol. No space is allowed after the "?". Blanks should be represented by "+".
<A HREF="">


6) Try using your cgi file by adding the parameters to the URL. See what happens if the parameters are not tea or coffee or hot chocolate.

7) Write an HTML file that does not contain a form but contains three links that send URLs with attached parameters "tea", "coffee", "hot chocolate" to the cgi file. (In this manner you can write cgi files that communicate with other cgi files on the web.)

8) (optional) Choose a search engine on the web and analyse how they attach the search paramaters to the URL. Write a form that sends data to that search engine.