Source code for the guestbook
Emmie Lewis
Your About.com Guide to Perl/PHP
How To Write A Guestbook Script in Perl
#===============================
# A Simple Guestbook Script
# Copyright 1999-2001, Emmie P. Lewis
# Created 07/24/01
# http://perl.about.com/library/weekly/aa072701a.htm
#===============================
# This example shows how to write
# a guestbook script, and includes all the
# files and source code needed for the project
#===============================
======================
add.html:
Sign My GuestBook
Sign My Guest Book
======================
guestbook.html
Welcome to My Guestbook
Welcome to My Guestbook
Add Your Name!
======================
guestbook.cgi
#!/usr/local/bin/perl
#===============================
# A Simple Guestbook Script
# Copyright 1999-2001, Emmie P. Lewis
# Created 07/24/01
#===============================
# This example code shows how to write
# a guestbook script.
#===============================
print "Content-type:text/html\n\n";
#===========================
# Initialize the variables
#===========================
# Relative path of Guestbook.html
$guestbook_file = "../guestbook.html";
# URL to guestbook.html
$guestbook_url = "http://www.yourdomain.com/guestbook.html";
# Initialize list of months
@month = ("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December");
#===========================
# Read the data from the form
#===========================
if($ENV{'REQUEST_METHOD'} eq "GET"){
$my_data = $ENV{'QUERY_STRING'};
}
else {
$data_length = $ENV{'CONTENT_LENGTH'};
$bytes_read = read(STDIN, $my_data, $data_length);
}
# Let's load it into something we can use
@name_value_array = split(/&/, $my_data);
# Here's where we do the actual work. We're going to cycle
# through @name_value_array to decode the name=value pairs
foreach $name_value_pair (@name_value_array) {
# Split the name=value pair in your HTML form data
($name, $value) = split(/=/, $name_value_pair);
# Now, replace '+' with ' '
$name =~ tr/+/ /;
$value =~ tr/+/ /;
# Next, we'll translate any hex values back into characters
$name =~ s/%(..)/pack("C",hex($1))/eg;
$value =~ s/%(..)/pack("C",hex($1))/eg;
# Finally, we'll load the variables into an associative array
# so we can use it when we need it.
if($form_data{$name})
{
$form_data{$name} .= "\t$value";
}
else
{
$form_data{$name} = $value;
}
}
#===========================
# Get the date and time
# information
#===========================
($Seconds, $Minutes, $Hours, $DayInMonth, $Month,
$ShortYear, $DayOfWeek, $DayOfYear, $IsDST) = localtime(time);
# Make the year four digits instead of just two
$Year = $ShortYear + 1900;
$EntryDate = "$month[$Month] $DayInMonth, $Year";
#===========================
# Open the file, using the
# variable set at the beginning
# of the script.
#===========================
open(GUESTBOOK, "<$guestbook_file") ||
die "Can't open GUESTBOOK: $guestbook_file\n";
# Read the entire file into a list so we can use it later
@guestbook =;
# ALWAYS close the file when you're done!
close(GUESTBOOK);
#===========================
# Now open it again, but this
# time we'll write to it
#===========================
open(GUESTBOOK, ">$guestbook_file") ||
die "Can't open GUESTBOOK: $guestbook_file\n";
#===========================
# Here's where we write it out
#===========================
foreach $line (@guestbook){
# Check to see if we're at the beginning of the guest book
if($line =~ //i) {
#===========================
# We are, so add our latest guest
# Start by adding the guestbook
# header to the file, so we can
# find it next time
#===========================
print GUESTBOOK "\n";
#===========================
# The guestbook is an HTML
# file, so we'll have to add
# the tags too
#===========================
print GUESTBOOK "\n";
#===========================
if($form_data{'email'}) {
# if there's an email address, let's add it as a link
print GUESTBOOK "-
$form_data{'name'} - $form_data{'city'},
$form_data{'location'}\n";
}
else {
# otherwise, just add their name
print GUESTBOOK "From: $form_data{'name'}
\n";
}
#===========================
#===========================
if($form_data{'web_url'}) {
# If they included their web url, add the link
print GUESTBOOK "- Homepage :
$form_data{'web_title'}\n";
}
#===========================
#===========================
# add the comments
print GUESTBOOK "
\n";
print GUESTBOOK "$form_data{'comments'}\n";
print GUESTBOOK "
\n";
#===========================
#===========================
# add the date
print GUESTBOOK "\n";
print GUESTBOOK "signed on $EntryDate\n";
print GUESTBOOK "\n";
#===========================
#===========================
# finish up
print GUESTBOOK "
\n";
print GUESTBOOK "
\n";
#===========================
}# end: if($line =~ //i)
else {
#===========================
# just print the existing
# lines back to the updated file
#===========================
print GUESTBOOK "$line";
}# end: else[if($line =~ //i)]
}# end: foreach $line (@guestbook) {
close(GUESTBOOK);
#===========================
# Let them know their entry
# is added
#===========================
print <<"EOF"
Your GuestBook Entry has been Submitted\!
Your guestbook entry has been successfully added to the
guestbook.
You may need to click reload to view your guestbook entry.
EOF
#===========================