#!/usr/local/bin/perl #=============================== # A Simple Guestbook Script # Copyright 1999, Emmie P. Lewis # Created 07/09/99 #=============================== # This script shows how to write # a guestbook script. #=============================== print "Content-type:text/html\n\n"; # Initialize File Pathnames # Relative path of Guestbook.html $guestbook_file = "../guestbook.html"; # URL to guestbook.html $guestbook_url = "http://localhost/guestbook.html"; # Initialize list of months @month = ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); # Read in the CGI Data, no matter which method was used # (If you're unsure what this code does, see 'Reading CGI Data' # http://perl.about.com/library/weekly/aa052499.htm) 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. $FormData{$name} = $value; } # Get the date and time information ($Seconds, $Minutes, $Hours, $DayInMonth, $Month, $ShortYear, $DayOfWeek, $DayOfYear, $IsDST) = localtime(time); # Fix the year to keep it Y2K compliant, as described in 'Perl and Y2K' # http://perl.about.com/library/weekly/aa070299.htm $Year = $ShortYear + 1900; $EntryDate = "$month[$Month] $DayInMonth, $Year"; # OK, now open the current guestbook file and read it in open(GUESTBOOK, "$guestbook_file") || die "Can't open GUESTBOOK: $guestbook_file\n"; @guestbook =; close(GUESTBOOK); # Open it again so that we can add the current guest to the file open(GUESTBOOK, ">$guestbook_file") || die "Can't open GUESTBOOK: $guestbook_file\n"; 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($FormData{'email'}) { # if there's an email address, let's add it as a link print GUESTBOOK "
$FormData{'name'} - $FormData{'city'}, $FormData{'location'}\n"; } else { # otherwise, just add their name print GUESTBOOK "From: $FormData{'name'}
\n"; } if($FormData{'web_url'}) { # If they included their web url, add the link print GUESTBOOK "
Homepage : $FormData{'web_title'}\n"; } # add the comments print GUESTBOOK "

    \n"; print GUESTBOOK "$FormData{'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"; } else { # just print the existing line to the updated file print GUESTBOOK "$line"; } } close(GUESTBOOK); # Tell our new guest they've been added, and let them see their entry print <<"EOF"


Your entry has been added to the guestbook. You may need to click reload to view your guestbook entry.