next up previous
Next: A Web Page Counter Up: Some useful Perl Previous: An Address Book Search

Creating a Guest Book

The Guest book is a bit more complicated than the address book. In a guestbook readers can post comments about you WWW pages.

Guestbooks are quite common on WWW pages.

The HTML Form is as follows:

<HTML>
<HEAD>
<TITLE>Comments!</TITLE>
</HEAD>
</BODY>

<!--GUESTBOOK-->
<H1>Comments!</H2>
<P>Here are comments people have left 
about my pages.  Post your own
using the form at the end of the page.
<P>Comments list started on
<!--STARTDATE--> Apr 4 1995 
Last post on
<!--LASTDATE-->  
Thu Aug 24 09:25:46 PDT 1995 
<HR><B>Susan M.
 <A HREF=mailto:sdsm>sus@monitor.com
</A></B>  
Tue Apr 10 05:57:09 EDT 1995
<P>This is the worst home page 
I have ever seen on the net.  Please
stop writing.
<HR><B>Tom Blanc 
 <A HREF=mailto:le7may@lne.com>
tlb666@netcom.com</A></B>  Wed Apr 11 
21:58:50 EDT 1995
<P>Dude.  Get some professional help.  
Now.


<!--POINTER-->
<!--everything after this is standard 
and unchanging. -->
<HR>
Post a response:
<BR>
<FORM METHOD=POST 
ACTION="http://www.cs.cf.ac.uk/
/User-bin/Dave.Marshallguestbook.pl/
guest.html">


Name: <INPUT TYPE="text" NAME="name" 
SIZE=25 MAXLENGTH=25>
<BR>
Email address: <INPUT TYPE="text" 
NAME="address" SIZE=30 MAXLENGTH=30>
<BR>
Text:
<BR>
<TEXTAREA ROWS=15 COLS=60 NAME="body">
</TEXTAREA>
<BR>
<INPUT TYPE=submit VALUE="POST">
<INPUT TYPE=reset VALUE="CLEAR">
</FORM>
<HR>
<ADDRESS>
This guestbook program copyright 1995 
<A HREF="http://www.lne.com/lemay/">
Laura Lemay</A> and
<A HREF="http://www.lne.com/ericm/">
Eric Murray</A>
</ADDRESS>
</BODY>
</HTML>

and looks like this:



This is just a from front end. Go to the THIS PAGE for a fully working form.


Comments!

Here are comments people have left about my pages. Post your own using the form at the end of the page.

Comments list started on Apr 4 1995 Last post on Thu Aug 24 09:25:46 PDT 1995


Susan M. sus@monitor.com Tue Apr 10 05:57:09 EDT 1995

This is the worst home page I have ever seen on the net. Please stop writing.


Tom Blanc tlb666@netcom.com Wed Apr 11 21:58:50 EDT 1995

Dude. Get some professional help. Now.


xyzzy xyzzy@zorkmid.com Wed Apr 11 22:06:25 EDT 1995

What is this? I can't figure out whats going on here.


Laura lemay@lne.com Mon Apr 24 00:21:49 EDT 1995

Hi Laura! I was just testing your awesome guestbook. Say hi to Eric for me.


Post a response:
Name:
Email address:
Text:


This guestbook program copyright 1995 Laura Lemay and Eric Murray

The Perl CGI is as follows:

require 'cgi-lib.pl';

# grab values passed from form:
&ReadParse(*in);

print "Content-type: text/html\n\n";

# print the top part of the response
print "<HTML><HEAD>\n";
print "<TITLE>Post Results</TITLE>\n";
print "</HEAD><BODY>\n";

# change to your favorite date format:
$date = `date`;
chop($date); # trim \n

# Grab the HTML file and make a file name for the temp file.
$file = "$ENV{'PWD'}" . "$ENV{'PATH_INFO'}";
$tmp = $file . ".tmp";
$tmp =~ s/\//@/g;	# make a unique tmp file name from the path
$tmp = "/tmp/$tmp";

# if any fields are blank, then skip the post and inform user:
if ( !$in{'name'} || !$in{'address'} || !$in{'body'}) {
    &err("You haven't filled in all the fields. Back up and try again.");
}

# reformat the body of the post.  we want to preserve paragraph breaks.
$text = $in{'body'};
$text =~ s/\n\r/\n/g;
$text =~ s/\r\r/<P>/g;
$text =~ s/\n\n/<P>/g;
$text =~ s/\n/ /g;
$text =~ s/<P><P>/<P>/g;

# get an exclusive open on the tmp file, so
# two posts at the same time don't clobber each other.
for($count = 0; -f "$tmp"; $count++) {
	# oh no. someone else is trying to update the message file.  so we wait.
	sleep(1);
	&err("Tmp file in use, giving up!") if ($count > 4);  # but not for long
}
open(TMP,">$tmp") || &err("Can't open tmp file.");
# open the HTML file
open(FILE,"<$file") || 
		&err("Can't open file $file: $!");

# an HTMLBBS file.  look through it for the HTML comments
# that denote stuff we want to change:
while(<FILE>) {
	if (/<!--LASTDATE-->/)  { print TMP "<!--LASTDATE-->  $date \n"; }
	elsif (/<!--GUESTBOOK-->/) {
		print TMP "<!--GUESTBOOK-->\n";
		$guestbook++;
	}
	elsif (/<!--POINTER-->/) {
		# add this post
		print TMP "<HR>";
		print TMP "<B>$in{'name'}  \n";
		print TMP " <A HREF=mailto:$in{'address'}>
                   $in{'address'}</A></B>  $date\n";
		print TMP "<P> $text\n<!--POINTER-->\n";
	}
	else { print TMP $_; }  # copy lines
}
if (! defined $guestbook) 
{ &err("not a Guestbook file!"); } 


# move the new file over the old:
open(TMP,"<$tmp") || &err("Can't open tmp file.");
# open the HTML file
open(FILE,">$file") || 
		&err("Can't open file $file: $!");

while(<TMP>) {
	print FILE $_;
}
close(FILE);
close(TMP);
unlink "$tmp";

# print the rest of the response HTML
print "<H1>Thank you!</H1>";
print "<P>Your comment has been added to the ";
print "<A HREF=\"/User/Dave.Marshall/guest.html\">guestbook</A>\n";
print "</BODY></HTML>\n";
1;

# if we got an error, print message, close & clean up
sub err {
	local($msg) = @_;
	print "$msg\n";
	close FILE;
	close TMP;
	unlink "$tmp";
	print "</BODY></HTML>\n";
	exit;
}





Download Source code: