home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / guestbook.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  4.6 KB  |  128 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. ' This is used to control whether or not we empty the guestbook at
  15. ' the first hit after midnight.  We do it just to keep the list
  16. ' short.  You'll probably want to set this to False
  17. Const bDeleteEntries = True
  18.  
  19. ' Allows us to easily clear the guestbook if we notice someone is
  20. ' getting rude!  All you need to do is pass it ?force=anything
  21. ' Possibly something else you might not want. To disable it comment
  22. ' out the Request.QueryString line and uncomment the "" one.
  23. Dim bForce
  24. bForce = Request.QueryString("force")
  25. 'bForce = ""
  26.  
  27. ' Now that we're done implementing features you probably won't want,
  28. ' let's get to the actual script...
  29. Dim strFile ' String variable to store the path / file we write to
  30. ' I use MapPath here to make the script somewhat location independent.
  31. ' If I specified the physical path, you'd need to edit it to run this
  32. ' on your own server.  This way it should work fine as long as it's in
  33. ' the same directory as the guestbook file.  The include line also
  34. ' needs to be changed if you change this!  This file needs to exist
  35. ' BEFORE you run this script!
  36. strFile = Server.MapPath("guestbook.txt")
  37.  
  38. ' If the script doesn't have anything posted to it we display the form
  39. ' otherwise we process the input and append it to the guestbook file.
  40. If Request.Form.Count = 0 Then
  41.     ' Display the entry form.
  42.     %>
  43.     <H3>Sign Our Guestbook:</H3>
  44.     <FORM ACTION="guestbook.asp" METHOD="post">
  45.     <TABLE>
  46.         <TR>
  47.         <TD ALIGN="right"><B>Name:</B></TD>
  48.         <TD><INPUT TYPE="text" NAME="name" SIZE="15"></INPUT></TD>
  49.         </TR>
  50.         <TR>
  51.         <TD ALIGN="right"><B>Comment:</B></TD>
  52.         <TD><INPUT TYPE="text" NAME="comment" SIZE="35"></INPUT></TD>
  53.         </TR>
  54.     </TABLE>
  55.     <INPUT TYPE="submit" VALUE="Sign Guestbook!"></INPUT>
  56.     </FORM>
  57.  
  58.     <BR>
  59.  
  60.     <H3>Today's Comments:</H3>
  61.     <!-- Instead of doing this in script, I simply include
  62.             the guestbook file as is -->
  63.     <!--#INCLUDE FILE="guestbook.txt"-->
  64.     <%
  65. Else
  66.     ' Log the entry to the guestbook file
  67.     Dim objFSO  'FileSystemObject Variable
  68.     Dim objFile 'File Object Variable
  69.     
  70.     ' Create an instance of the FileSystemObject
  71.     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  72.     ' Open the TextFile (FileName, ForAppending, AllowCreation)
  73.     Set objFile = objFSO.OpenTextFile(strFile, 8, True)
  74.  
  75.     ' Log the results
  76.     ' I simply bold the name and do a <BR>.
  77.     ' You can make it look however you'd like.
  78.     ' Once again I remind readers that we by no means claim to
  79.     ' be UI experts.  Although one person did ask us if we had a
  80.     ' graphic designer!  I laughed so hard that I almost hurt myself!
  81.     objFile.Write "<B>"
  82.     objFile.Write Server.HTMLEncode(Request.Form("name"))
  83.     objFile.Write ":</B> "
  84.     objFile.Write Server.HTMLEncode(Request.Form("comment"))
  85.     objFile.Write "<BR>"
  86.     objFile.WriteLine ""
  87.     
  88.     ' Close the file and dispose of our objects
  89.     objFile.Close
  90.     Set objFile = Nothing
  91.     Set objFSO = Nothing
  92.  
  93.     ' Tell people we've written their info
  94.     %>
  95.     <H3>Your comments have been written to the file!</H3>
  96.     <A HREF="./guestbook.asp">Back to the guestbook</A>
  97.     <%
  98. End If
  99.  
  100. ' We do this to delete the file every day to keep it managable.
  101. ' If you were doing this for real you probably wouldn't want to
  102. ' do this so we have defined a const named bDeleteEntries at the
  103. ' top of the script that you can set to False to prevent this
  104. ' section from running.  You could also delete this whole
  105. ' If Then....End If block if you'd like.  Just be sure to leave
  106. ' the script delimiter at the bottom!
  107. If bDeleteEntries Then
  108.     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
  109.     Set objFile = objFSO.GetFile(strFile)
  110.     If DateDiff("d", objFile.DateLastModified, Date()) <> 0 Or bForce <> "" Then
  111.         Set objFile = Nothing        
  112.         ' Can't use delete because we need the file to exist for
  113.         ' the include the next time the script is run!
  114.         'objFile.Delete
  115.         
  116.         ' Create a file overwriting old one.
  117.         Set objFile = objFSO.CreateTextFile(strFile, True)
  118.  
  119.         ' The include barks if the file's empty!
  120.         objFile.Write "<B>John:</B> "
  121.         objFile.WriteLine "I hope you like our guestbook!<BR>"
  122.         objFile.Close
  123.     End If
  124.     Set objFile = Nothing
  125.     Set objFSO = Nothing
  126. End If
  127. %>
  128.