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.
browser | server |
user requests html document | |
server finds HTML file and sends page back |
CGI:
browser | server |
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="submit" value="Place order">
<input type="radio" name="drink" value="coffee" > Coffee <br>
<input type="radio" name="drink" value="hot chocolate" > Hot Chocolate
<p>
</form>
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.
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.