Creating a GuestbookBy Joshua Bengal
When I first started writing web pages in college (good ole' Lynx), I was fairly amazed that anyone signed my guestbook. At that time, Matt's Script Archive had the best free guestbook out there, and it ran fine on my university's Unix system. However, after graduating, I didn't have any easy access to a Unix server, and since I was learning ASP, I had NT server space out the wazzo. So I decided to take a hack at writing my own ASP guestbook after reading this article on file manipulation in ASP. Lets take a look at it.
There are four files needed for the guestbook to work:
addguest.html- This is the form the user will enter his information on. You can include whatever field you like. For this example I used Name, Email, City, State, Zip, Country, Web Page, and Comment.
submit.asp - Records the guestbook entry.
entries.dat - A text file that holds the guestbook entries.
guestbook.asp - Displays the guestbook.
Let's go through these one by one!
<FORM> tag. It should look something like this:
This sends the information the user enters to our next file,
submit.asp. (Note the
property in the
This is the file that records your entries. Let's go through it step by step, shall we?
Learn it, love it, live it. (To learn more about
Option Explicit be sure to read:
These are the constants you need to work with the files. Just saves you from having to use the numbers directly, and makes the code a little more readable.
These are the variables we're going to work with.
This bit of code creates our
FileSystemObject, which allows us to work with the files needed. We
GetFile method to open up out file
entries.dat and read all the existing
entries into the variable
entries (I use real complex variable names, don't I?). If you are not
familiar with the
FileSystemObject, be sure to take a moment to read the great
Now you may be wondering why we're reading our previous entries first. This is done to make sure the last person to sign the book will show up on top. If we appended, it would show up on bottom, and if we just wrote it out to the file, it would overwrite the old entries and just leave the last one entered. So we're going to copy the old entries, write the new one, and the append the old ones. There's probably a more suave way to accomplish this, but hey, I don't got all day! :-)
This reads the information the user entered into the form. This section would be changed depending on the
fields you decided to go with in your
addguest.html. Notice I also included a signed variable to
record the date and time the user signed the guestbook.
Here we begin writing out our information. We're going to overwrite the existing
out new information, so we use the
CreateTextFile method to do so.
"But wait, Josh! That's not very safe! If something happened to the server during this operation, we would lose
all our entries! Why would you possibly write such code, when you possess such awesome powers of foresight and
Well, to answer my critic here, yes, I guess there is that .000001% chance that a power outage or something during the exact millisecond that this operation was running would dump your variables after you overwrote the file, and then technically, yes you would lose your guestbook. If you are so paranoid that this will happen, you could read more on file manipulation in ASP and make a copy of your old entries and work with that. However, If you're worried that much about guestbook entries on your web page, I suspect you have deeper issues to deal with.
Anyway, back to writing our entries. I'm a big fan of the almost standard
<HR> between entries,
so lets write one out:
Wasn't that fun?
ObjFile.Writeline is pretty much a
Response.Write for the file open,
in this case the newly created
entries.dat. So lets go ahead and write out the rest of the entry, shall
Now that we have the current entry written out, lets write out all the other ones we read in earlier
And the we finish up
entries.dat contains our new entry, followed by all the previous entries. Now we just
need to see it. And so, we move on to ...
In Part 2 we'll look at the source for
ASP page that displays the guestbook!