To read the article online, visit

Creating a Guestbook

By 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:

    1.) 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.

    2.) submit.asp - Records the guestbook entry.

    3.) entries.dat - A text file that holds the guestbook entries.

    4.) guestbook.asp - Displays the guestbook.

Let's go through these one by one!

This is a standard html page that displays a form that the user can use to fill in information. You can choose whatever fields you want, you can add javascript verification to make sure certain fields are not left blank, or apply any other client-side form field validation you'd like. Only one line is really important, and that's the <FORM> tag. It should look something like this:

<form method="POST" action="submit.asp">

This sends the information the user enters to our next file, submit.asp. (Note the ACTION property in the FORM tag.)

This is the file that records your entries. Let's go through it step by step, shall we?

<% Option Explicit

Learn it, love it, live it. (To learn more about Option Explicit be sure to read: Using Option Explicit.)

  Const ForReading = 1, ForWriting = 2, ForAppending = 8

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.

  'declare variable
  Dim objFS, objFile, entries, textstream
  Dim name, email, city, state, country, url, comment, signed

These are the variables we're going to work with.

  'read guestbook entries into textstream
  Set objFS = Server.CreateObject ("Scripting.FileSystemObject")
  set objFile = objFS.GetFile(server.mappath("entries.dat"))
  set textstream = objFile.OpenAsTextStream(ForReading, -2)
  entries = textstream.ReadAll

This bit of code creates our FileSystemObject, which allows us to work with the files needed. We user the 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 FileSystemObject F.A.Q.!

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! :-)

  'read data entered into form
  name = Request.Form("name")
  email = Request.Form("email")
  city = Request.Form("city")
  state = Request.Form("state")
  country = Request.Form("country")
  url = Request.Form("url")
  comment = Request.Form("comment")
  signed = FormatDateTime(Date, 1) & " " & Time

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.

  'write out new entry
  set objFile = objFS.CreateTextFile(server.mappath("entries.dat"), true)

Here we begin writing out our information. We're going to overwrite the existing entries.dat with 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 wisdom? Why?"

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:

  objFile.WriteLine("<hr noshade>")

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 we?

  objFile.WriteLine("<font face=Verdana size=2>" & comment & "<p>")

  'print name if entered
  if Not(name = "") then objFile.WriteLine(name & "<br> ")

  'print URL if entered
  if Not(url = "") then objFile.WriteLine(url & "<Br> ")

  'print email if entered
  if Not(email = "") then objFile.WriteLine(email & "<Br> ")

  'print city if entered
  if Not(city = "") then objFile.WriteLine(city & ", ")

  'print state if entered
  if Not(state = "") then objFile.WriteLine(state & " ")

  'print country if entered
  if Not(country = "") then objFile.WriteLine(country & " ")

  'print date signed
  objFile.WriteLine("- " & signed & "</font>")

Now that we have the current entry written out, lets write out all the other ones we read in earlier

  'write out former entries

And the we finish up

  'close file

Now our 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 ...

  response.redirect ("guestbook.asp")

In Part 2 we'll look at the source for Guestbook.asp, the ASP page that displays the guestbook!

  • Read Part 2

  • Article Information
    Article Title: Creating a Guestbook
    Article Author: Joshua Bengal
    Published Date: Thursday, June 29, 2000
    Article URL:

    Copyright 2019 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers