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
http://ella.slis.indiana.edu/~username/filename. The URL for cgi files is
http://ella.slis.indiana.edu/~username/cgi/filename
(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.)

Exercises

1) Save some simple html code such as

<HTML>
<HEAD>
<TITLE> Hello World</TITLE>
</HEAD>
<BODY>
<H1>Greetings</H1>
</BODY>
</HTML>
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
#
# CGI
#
use CGI qw(:standard);
print header();
print "<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
</HEAD>
<BODY>
<H1>Greetings</H1>
</BODY>
</HTML>";
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:

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

CGI:

browserserver
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

Exercises

A sample form looks like this:

<form action="http://ella.slis.indiana.edu/~username/cgi/example" 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">
</form>

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>
<head>
<title> Tea is served</title>
</head>
<body>
<hr><h1> Tea</h1><hr>
<p>";

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 "
<p>
Thank you for your visit. Please come again.
<p>
<hr>
</body>
</html>
";

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="http://ella.slis.indiana.edu/~upriss/cgi/example?drink=tea">
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="http://ella.slis.indiana.edu/~upriss/cgi/example?drink=hot+chocolate">

Exercises

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.