![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
Editing the Guest BookAs you know by now, any time you open a site to interaction from visitors, you open it to unwanted interaction as well. It would be nice if everyone just filled out the forms the way they were supposed to, but it never happens that way. The most common problem youll encounter with form information is when its entered incorrectly. Then, there will always be immature individuals who will salt your form databases with their idea of humor. Amusing or not, the stuff shouldnt be there. A case in point is your guest book. This is a module of Web pages that anyone can visit, so if something gets into it that shouldnt be there, anyone will see it. How can you take unwanted entries out of the guest book? Through Perl and CGI, you can display all of the guest book entries in a password-protected page and delete the ones you dont want, then write the edited list back to its database file. This project can be the starting point for many similar maintenance tasksthe basic concepts begin here. You can begin by creating a special directory in your Web servers root called maint. Move your password file into it first, then make password.html the default HTML document in the maint directory. Your Web server will determine how you turn the password gateway page into the defaultthere are several conventions. Most UNIX servers, given a URL that is just a directory path, will go to that directory, look for a file called index.html, and load it if its there. However, the Sambar server wants the file to be named index.htm, and Internet Information Server prefers default.htm (though it can be configured for index.htm). Now, when you enter the URL for your Web site with the path maint appended to it (such as www.WebSite.com/maint), youll go straight to your password page. Youve created a gateway to a maintenance directory that, for now, only you can enter. Using Perl to Delete Guest Book Entries Perl deals with lists very adeptly, an attribute you can use to display all of the entries in the guest book, then flag the ones you want to erase. You will need two scripts for this project, one to display the entries and another to write them back into their file, skipping the ones you have marked for extinction. The following Perl code is the first script: #!/perl/bin/perl # entries.pl # Displays guest book entries in an HTML document # that includes form checkboxes to flag an entry # or multiple entries for deletion. # Get the header file; complain and exit if it cant be # found. Otherwise, define the title string and the Perl # script that will do the deletion. require ("c:/Program Files/Sambar/cgi-bin/GuestBook.pm") || die ("Cant find GuestBook header file: $!\n"); $Title = "Editing the guest book"; $DelScript = "/cgi-bin/delguest.pl"; # Attempt to open the guest book file. Again, this is # a fatal error if it doesnt succeed. open (GUEST_LOG, $GuestBookPath) || die "Cant open guest book: $!"; # Set up the HTML document as a form this time. &HTML_Header ($Title); print "<BODY>\n"; print "<H1 ALIGN=\"CENTER\">$Title</H1>\n"; print "<HR>\n"; print "<FORM ACTION=\"$DelScript\" METHOD=\"POST\">\n"; # Do a limited display of the records -- name and entry time -- # along with a checkbox given the index of the record as its # value. $index = 0; # An index counter. while (read (GUEST_LOG, $buffer, $GuestEntrySize)) { @InfoArray = unpack ($GuestEntryStruct, $buffer); for ($n = 0; $n < ($NumElements - 1); $n++) { $InfoArray[$n] =~ s/\0//g; } # Get all of the fields, though well only use three # of them here. ($FirstName, $LastName, $City, $State, $Country, $Email, $Comments, $NumAccessTime) = @InfoArray; # Display the name and formatted time. print "<STRONG>$FirstName $LastName<BR>\n"; ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime ($NumAccessTime); print "$WeekDay[$wday], $Month[$mon] $mday, ", $year + 1900; print " at $hour:"; if ($min < 10) { print "0"; } print "$min:"; if ($sec < 10) { print "0"; } print "$sec </STRONG><BR>\n"; # Set a checkbox to $n. print "Delete? <INPUT TYPE=\"checkbox\"\n"; print "NAME=\"delete\" VALUE=\"$index\">\n"; print "<HR>\n"; $index++; # Increment $index for the next pass. } # End while (read ) # Form submit and reset buttons at the bottom. print "<INPUT TYPE=\"submit\" VALUE=\"Delete\">\n"; print "<INPUT TYPE=\"reset\" VALUE=\"Clear\">\n"; # Clean up the HTML and file and exit. print "</FORM>\n</BODY>\n"; &HTML_Footer; close (GUEST_LOG); # End entries.pl Save the script as entries.pl. For now, you can run it as a URL in your Web browser and it will display a screen similar to the one illustrated in Figure 13.13.
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |