To set up a guestbook, usually we need at least three files:
<html> <head><title>Add to My Guestbook</title></head> <body> <center><h1>Add to My Guestbook</h1></center> Fill in the blanks below to add to my guestbook. The only blanks that you have to fill in are the comments and name section. Thanks! Click here to <a href="guests.html">View Guestbook</a> I am keeping. <hr> <form method=POST action="http://bau.cba.uh.edu/CGITutorial/guestbook.pl"> <pre> Your Name: <input type=text name=name size=30> E-Mail : <input type=text name=email size=30> URL : <input type=text name=url size=40> Comments : <textarea name=suggest COLS=40 ROWS=3></textarea><p> <input type=submit value="Add Guestbook"> <input type=reset> </pre> </form></body> </html>
<html> <head><title>Guestbook</title></head> <body bgcolor=#e3fbe6> <center> <h1><img src="pencil.gif"> <font color=red>Guestbook</font></h1> </center> <h4>Thank you for visiting our pages. We would love it if you would <a href="addguest.html">Add</a> to this guestbook we are keeping!</h4> <hr> <!--begin--> <p> <img src="ball.gif"><b>Welcome to sign the guestbook that we are keeping.</b><br> <p><a href="http://www.cs.uh.edu/~zsyu">Zhanshou Yu</a> <a href="mailto:zyu@bayou.uh.edu"><i>zyu@bayou.uh.edu</i></a><br> Date :Tue Jan 20 10:31:34 CST 1998 <br><hr> <i>1/8/1998, College of Business Administration</i> </body> </html>This is a very common HTML file except the <!--begin--> statement. <!--begin--> is a HTML comment statement. It acts as an ancher. Each time to add a new guest, we need to know the exact postion where to insert. A special comment statement like <!--begin--> works pretty well in this case. If we suppose the newest message is inserted in the top of the guestbook like in our example, the <!--begin--> statement should be in the top of the guestbook.
(1) #!/usr/local/bin/perl ######################################################################## # guestbook.pl--Guestbook # 1/6/98 by Zhanshou Yu # Any technical comment please send to : # zyu@bayou.uh.edu ######################################################################## (2) # Global variable , specify the guestbook path $file= "/home/httpd/htdocs/CGITutorial/guests.html"; (3) # Get the input from form read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'}); (4) #Split the name-value pairs. @pairs=split(/&/,$buffer); #for each name=value pair, seperate them. foreach $pair(@pairs){ ($name,$value)=split(/=/,$pair); #split name=value to name value $value=~tr/+/ /; #substitute plus sign with space sign $value=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; #decode hexdecimal to character $FORM{$name}=$value; } (5) #------print the return HTML------------------ #print the MIME type print"Content-type: text/html\n\n"; (6) #Check whether missing Your name and suggestion field &missing_field('Your Name') unless ($FORM{'name'}); &missing_field('Your suggestion') unless ($FORM{'suggest'}); (7) #Get the system date $date=`/usr/bin/date`; chop($date); #chop the last enter character (8) # Open the guestbook file and store it in an array open (FILE,"$file") || die "Can't Open $file: $!\n"; #put each line of the file to an array @LINES @LINES=<FILE>; close(FILE); $SIZE=@LINES; #$SIZE is the number of lines in the guestbook (9) # Open link File to Output open (FILE2,">$file") || die "Can't Open $file $!\n"; (10) #Print out the Acknowlegement message print "<html><head><Title>Thank you for Signing the Guestbook </Title></head>\n"; print "<body bgcolor=#e3fbc6>\n"; print "<center><h1>Thank you for Signing the Guestbook </h1></center>\n"; print " Thank you for filling in the guestbook. Your entry has been added to the guestbook.<hr>"; print " Here is what you submitted:"; (11) #Add the guest information to the guestbook for ($i=0;$i<=$SIZE;$i++) { $_=$LINES[$i]; if (/<!--begin-->/) { print FILE2 "<!--begin-->\n"; print FILE2 "<p> <img src=\"ball.gif\"><b>$FORM{'suggest'}</b><br>\n"; print "<p><b>$FORM{'suggest'}</b><br>\n"; if($FORM{'url'}) { print FILE2 "<p><a href=\"$FORM{'url'}\">$FORM{'name'}</a> \n"; print "<p><a href=\"$FORM{'url'}\">$FORM{'name'}</a>\n"; } else { print FILE2 "<p>$FORM{'name'}\n"; print "<p>$FORM{'name'}\n"; } if($FORM{'email'}) { print FILE2 " <a href=\"mailto:$FORM{'email'}\"><i>$FORM{'email'}</i></a><br> \n"; print " <a href=\"mailto:$FORM{'email'}\"><i>$FORM{'email'}</i></a><br> \n"; } print FILE2 " Date :$date <br><hr>\n"; print " Date :$date <br><hr>\n"; } else { print FILE2 $_; } } (12) close (FILE2); print "Come here to <a href=\"guests.html\">View </a> the guestbook."; print "</body></html>"; (13) #missing_field subrountine. If mission field, print out error message. sub missing_field { local($variable)=@_; print "<html><head><Title>Guest Information Error</Title></head>"; print "<center><h1> Guest Infroamtion Error!</h1></center>"; print "<center> <TABLE width=\"50%\">"; print" <TR><TD bgcolor=#007000 align=center><FONT color=#00FF9F>"; print" You did not fill the <i>$variable </i>field.\n"; print" Please go back to the form and fill out it. Thank you.\n"; print "</FONT></TD></TR> </TABLE></center>"; exit; }
(1). Invoke the Perl interpreter.
(2). Specify the guestbook's path and file name, which is "/home/httpd/htdocs/CGITutorial/guests.html" in server bau.cba.uh.edu.
(3). Get the input string from the form.
(4). Decode and parsing the input string.
(5). Print the HTML header.
(6). Call the &missing_field subrountine to print error message if Your Name or Suggestion field is missing.
(7). Get the system time.
(8). Open the guestbook and store the file in an temp array @LINE:
@LINES=<FILE>;which puts the whole guestbook file in an string array line by line. Notes the statement:
$SIZE=@LINES;gets the number of element in the array, here it is easily to know that the $SIZE represents the number of lines in the guestbook file.
(9). Open the guestbook again. This time we open it not for storing the file in an temp array. But to add the new guest comments.
(10). This is nothing but to print out the Acknowlegement message
(11). Now it is time to add the new guest comments. We check each line of the guests.html, if find <!--begin-->, then add the new guest following the <!--begin--> statement, other old guests are still the same:
for ($i=0;$i<=$SIZE;$i++) { $_=$LINES[$i]; if (/<!--begin-->/) { #add the new guest comments right here ... } else { print FILE2 $_; #copy the old guests comments } }(12). After finish editing the guestbook, Close the file.
(13). missing_field is a subrountine to print missing field error message.