In general, you need two directories: "public_html" and a subdirectory of "public_html" called "cgi-bin". The public_html directory should be world readable and executable. The cgi-bin directory should be executable but not readable. You may also need to change the permissions for your top level directory. The following commands create the directories and set the permissions. You should copy and paste the following commands at the Unix prompt (not as a Perl script). You need to do this only once. If you already have a public_html directory, skip the second line.
cd; chmod 711 .;
mkdir public_html; chmod 755 public_html;
cd public_html; mkdir cgi-bin; chmod 711 cgi-bin;
All html files should be in the public_html directory,
all cgi files in the cgi-bin directory. The URL for
the html files is
http://www.student.soc.napier.ac.uk/~username/filename.
The URL for cgi files is
http://www.student.soc.napier.ac.uk/~username/cgi-bin/filename
<html>
<head><title>Hello World</title></head>
<body>
<h1>Greetings</h1>
</body></html>
as a file in your public_html directory on the webserver. The file permissions of this file should be -rw-r--r--. Type ls -l to check the file permissions. You can set the permissions with chmod 644 filename. Now, look at the file through your browser.
2) Save some simple cgi code such as
#!/usr/local/bin/perl -w
########################
use CGI qw(:standard -debug);
########################
print header();
print <<EOS;
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Greetings</h1>
</body></html>
EOS
# if you are editing this on a PC, you'll need this line after EOS
Notes:
use CGI::Carp qw( fatalsToBrowser );
######## for debugging info in the browser
$CGI::POST_MAX=1024 * 100;
######## max 100K posts
$CGI::DISABLE_UPLOADS = 1;
######## no uploads
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 |
<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>
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.
Optional Exercise:
Incorporate both the form and the CGI into the same file.
Select a picture:
<p>
<A HREF="http://www.student.soc.napier.ac.uk/~username/cgi-bin/greeting?image=1">
<img src="image1.jpg"></a><p>
<A HREF="http://www.student.soc.napier.ac.uk/~username/cgi-bin/greeting?image=2">
<img src="image2.jpg"></a><p>
<A HREF="http://www.student.soc.napier.ac.uk/~username/cgi-bin/greeting?image=3">
<img src="image3.jpg"></a><p>
<A HREF="http://www.student.soc.napier.ac.uk/~username/cgi-bin/greeting?image=4">
<img src="image4.jpg"></a><p>
All four URLs link to the same CGI file ("greeting") which uses a parameter ("image") to distinguish between the four images. This cgi file should produce a form with textboxes for the recipient's email address, the recipient's name and the message.
Upon submitting this form a second CGI file is invoked, which sends an email to the recipient. The code below gives some indication about how to send an email. Note: sending email messages is a potential security risk. At Napier you can send emails from cgi scripts but only within the firewall. The code below is purposefully provided in a form which will not work on DCS. To use this code you need to know enough about Unix to make some modifications. All user input needs to be checked carefully before sending mail. There are Perl libraries (Mail::CheckUser or Mail::Verify), which you can download from cpan.org. If you don't know how to do this, then don't use email in cgi scripts.
open(SENDMAIL, "|/usr/lib/sendmail -oi -t)
or die "Can't fork for sendmail: $!\n";
print SENDMAIL <<EOF;
To: somename\@napier.ac.uk
From: User Originating Mail <yourname\@napier.ac.uk>
Subject: A relevant subject line
Body of the message goes here, in as many lines as you like.
EOF
close(SENDMAIL);
7) The greeting card example shows that parameters can be added to a URL (for example "greeting?image=3"). Try what happens if you enter parameter values other than 1, 2, 3 or 4. Add an if statement to your cgi script that prints "not an acceptable selection" if the image number is not 1, 2, 3 or 4.