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.servername/~username/filename.
The URL for cgi files is
http://www.servername/~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
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
#!/usr/local/bin/perl -Tw
####################################################################
use CGI qw(:standard -debug);
use HTML::Template;
####################################################################
my $template_text = <<EOS;
<html>
<head><title>Hello World</title></head>
<body>
<h1>Greetings</h1>
</body></html>
EOS
####################################################################
my $cgi = CGI->new();
my $template = HTML::Template->new(scalarref => \$template_text );
print $cgi->header();
print $template->output();
To run this script on the command-line type "perl -T" and then Ctrl-D.
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>
#!/usr/local/bin/perl -Tw
####################################################################
use CGI qw(:standard -debug);
use HTML::Template;
####################################################################
my $template_text = <<EOS;
<html>
<head><title> Tea is served</title>
</head><body>
<hr><h1> Tea Room</h1><hr><p>
<TMPL_IF NAME="drink">
You requested <TMPL_VAR NAME="drink">.
<TMPL_ELSE>
An error occurred.
</TMPL_IF>
<p>Thank you for your visit. Please come again.<p><hr>
</body></html>
EOS
################## CGI and Template start #########################
my $cgi = CGI->new();
my $template = HTML::Template->new(scalarref => \$template_text );
print $cgi->header();
################## Variables that are used in template #############
my $drink = $cgi->param('drink');
if ($drink eq "tea" or $drink eq "coffee" or $drink eq "hot chocolate")
{$template->param(drink => $drink);}
################## Template is printed to browser ################
print $template->output();
(Documentation for HTML::Template can be found here.)
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.
Using HTML::Template, you'll need something like
my $template1 = HTML::Template->new(scalarref => \$template_text1 );
my $template2 = HTML::Template->new(scalarref => \$template_text2 );
and
if ($cgi->param('drink')) {
print $template1->output();
} else {
print $template2->output();
}
Select a picture:
<p>
<A HREF="http://www.servername/~username/cgi-bin/greeting?image=1">
<img src="image1.jpg"></a><p>
<A HREF="http://www.servername/~username/cgi-bin/greeting?image=2">
<img src="image2.jpg"></a><p>
<A HREF="http://www.servername/~username/cgi-bin/greeting?image=3">
<img src="image3.jpg"></a><p>
<A HREF="http://www.servername/~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. 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 or don't understand the security issues involved, 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.