Print this Article (NS4)
Netscape Navigator
Internet Explorer
Opera
Neoplanet

Forums
HTML
General
Site Dev
Programming
Flash
Grafix (Art)

Laboratory
Smart HTML
Color Lab
Generators

Contents
Simple CGI
  1. Hello World
  2. Print function
  3. Quoting, EOF
  4. Metacharacters
  5. Special Characters
Perl Basics 1
  1. Variables
  2. Arrays
  3. Hashes
  4. Split function
  5. Subroutines
  6. Defaults
Form CGI
  1. Loops
  2. Conditions
  3. Boolean Statements
  4. Pattern Matching
Time CGI
  1. Local Time
  2. GM Time
  3. time function
Perl Basics 2
  1. Reading Files
  2. Writing Files
  3. Including Files
  4. chop function
  5. chomp function
Guestbook CGI

Redirect CGI

Poll CGI

  1. Giving Commands
  2. Voting
  3. Results Display
  4. Adding Your Vote
Password CGI
  1. Authentification
  2. Multiple Users
  3. Encryption
Mailing List CGI
  1. Sendmail
  2. Multiple Recipients
Unlimited Subdomains CGI

News Grabber CGI

  1. LWP::Simple
Message Board CGI (Part 1)

Back to the Top


Perl Case Study - Guestbook CGI
By Lisa Hui

Ah, the age old guestbook program, which happens to be one of the most common applications of perl programs "for the masses." For this you only need to know the basics of writing to a file (see Perl Basics: Reading, Writing, and Taking a Bite) and a bit of knowledge from our Time CGI Case Study and you'll be able to write a guestbook script like a pro! No more need to use one of those remotely hosted ones - you modify and add your own features as you please :)

What the guestbook will consist of the following: a single HTML page onto which the entries are written, a posting page (with the form to sign the guestbook) and the CGI script which handles the updating of the guestbook itself. Let's name our HTML page something pretty obviously like guestbook.html and the form page to sign the book something like sign.html. Our script is guestbook.cgi, and it would merely take in the form values submitted through the sign.html page, format and post this information in guestbook.html. So it's very linear in structure :)

sign.html -------> guestbook.cgi --------> guestbook.html

Let's zoom in on what's going on in guestbook.cgi:

#!/usr/local/bin/perl
#################
# File Name: guestbook.cgi

#################
# Get the new entry from the form submitted through sign.html
if ($ENV{"REQUEST_METHOD"} eq 'GET') { $buffer = $ENV{'QUERY_STRING'}; }
else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }

@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $FORM{$name} = $value;
}

open(GB, "guestbook.html");
   @guestbook = <GB>;
close(GB);

Now we get to the harder part, we have the guestbook.html page where we want to insert a new entry...but not exactly at the top of the page. So we wouldn't first start by rewriting guestbook.html with the latest entry. In this case, we might want to stick a comment in the guestbook.html file, like <!-- begin --> just before the entries begin. Now

open(GB, ">guestbook.html");
   foreach $entry(@guestbook) {
      chomp($entry);
      if (entry =~ /<\!-- begin -->/) {
          print GB "<!-- begin -->\n";
          #add new entry here
          print GB "<b>Name:</b> $FORM{'name'}<br>\n";
          &getDate;
          print GB "<b>Date:</b> $date<br>\n";
          print GB "<b>Message:</b> $FORM{'message'}\n";
          print GB "<hr size=1 width=85%>\b";
      }
      else { print GB "$entry\n"; }
}

###################
# Print out a success message to the browser

print "Content-type: text/html\n\n";
print "Success! View the updated <a href=\"guestbook.html\">guestbook</a> page here.\n";

#################
# Get Today's Date from the Server (see Time CGI)

sub getDate {

   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

   $ampm = "a.m.";

   if ($hour eq 12) { $ampm = "p.m."; }
   if ($hour eq 0) { $hour = "12"; }
   if ($hour > 12) {
      $ampm = "p.m.";
      $hour = ($hour - 12);
   }

   @months = ("January","February","March","April","May","June","July","August","September","October","November","December");
   $date = "$months[$mon] $mday, 19$year at $hour\:$min $ampm EST";

}


And that's it! A very simple routine that doesn't require server side includes in the implementation like many other guestbook scripts do.

View Guestbook
Sign Guestbook

Last Updated August 4, 1999


Perl Case Study - Redirect CGI

©1999 Team 26297 "Ad Infinitum Web." All rights reserved. Any reproduction of this document for commercial or redistribution purposes without the permission of the author is forbidden.