To create our guestbook, we will use one file that has "entries" in it. It will be called guestbook.html and will look like this:
<HR> This is an entry in the guestbook<BR> <BR> Thank You<BR> <BR> <B>John Doe<BR> Anytown, USA</B> <HR> This is another entry.<BR> <BR> Bye<BR> <BR> <B>Jane Doe<BR> Bigtown, USA</B>
The <HR> seperates each entry, and each line will end in a <BR>. The entries are "signed" at the bottom in bold. We will also create the form for adding an entry in the guestbook:
<FORM METHOD=POST ACTION="/cgi-bin/guestbook.pl"> Name:<INPUT NAME="name"><BR> Location:<INPUT NAME="location"><BR> <BR>Comments:<BR> <TEXTAREA NAME="comments"> </TEXTAREA> <INPUT TYPE="SUBMIT"> </FORM>
Now we will write the script to do the magic
#!/usr/bin/perl #full path for the guestbook file $file="/home/httpd/html/guestbook.html"; require "cgi-lib.pl"; &ReadParse; #translate all the linefeeds into <BR>s for nice formatting $in{'comments'} =~ s/\n/<BR>\n/g; #open the guestbook file and append data to the end of it open(GUESTBOOK,">>$file"); #print the information print GUESTBOOK "\n<UL>\n"; print GUESTBOOK $in{'comments'}; print GUESTBOOK "\n<B>$in{'name'}<BR>\n"; print GUESTBOOK "$in{'location'}</B><BR>\n"; close GUESTBOOK;
All this script does is open the guestbook.html file and add the comments from the form (with some nice html formatting). Lets note a few things in this script: