| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Writing Your Own Guestbook in ASP - Part I
For the complete version of this tutorials, see http://www.herongyang.com/asp/hybook.html
------------ Writing Your Own Guestbook in ASP - Part I If you are interesting writing a guestbook in ASP on your Web site, you may learn from this tutorial. I wanted to provide a guestbook tool to allow visitors to share their comments with other visitors and myself. So I tarted to put together a simple guestbook application in ASP with MS Access database. While doing this, I have recorded all interesting notes and formatted as a tutorial to share with you. Let me first provide you some design highlights before going into any details. Functional requirements: hyBook should allow visitors to view all existing comments collected in the guestbook. hyBook should allow visitors to enter new comments anonymously. hyBook should organize comments by topics. hyBook should allow Webmasters to manage topics and comments. Technical highlights: Two tables will be used in hyBook: "Topic" stores topics, and "Comment" stores comments. Both tables will be stored in a MS Access database file. A single ASP page will be used for visitors to enter new comment and view existing comments. Two ASP page will be used for Webmaster to manage topics and comments. All ASP pages should be as secure as possible. All ASP pages should be configurable to allow Webmaster to merge into existing Website layout and style. Database Tables 1. "Topic" table will be named as "hyTopic" with the following fields: Code:
ID - "Autonumber" type Subject - "Text" type Content - "Memo" type 2. "Comment" table will be named as "hyComment" with the following fields: Code:
ID - "Autonumber" type TopicID - "Number" type, foreign key pointing to "hyTopic" table. Content - "Memo" type Name - "Text" type Email - "Text" type Timestamp - "Date" type IpAddress - "Text" type Table names are prefixed with "hy" so that they can be merged easily with your existing databases if needed. Configuration File The easiest way to set up configuration files with ASP pages is to use include files. For hyBook, I used one include file, _config.inc, to maintain all configurable items: Code:
<%
' _config.inc
'
' Configuration file
' hyBook version 2006.01.01
' Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/
' Database connection
Dim ogConn
Set ogConn = Server.CreateObject("ADODB.Connection")
' Number of submits per IP per day
Dim ngSubmitLimit
ngSubmitLimit = 10
' Default topic ID
Dim ngDefaultTopicID
ngDefaultTopicID = 14
' Page title
Dim sgPageTitle
sgPageTitle = "hyBook Demo"
' Debugging flag
Dim bgDebug, ogDebug
bgDebug = False
Set ogDebug = Nothing
Sub dbConnect
ogConn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.MapPath("/cgi-bin/hyBook.mdb")
If bgDebug Then
Set oFileSys = Server.CreateObject("Scripting.FileSystemObject")
Set ogDebug = oFileSys.OpenTextFile("\temp\hyBook.log", 8, True)
ogDebug.WriteLine("Open data base connection.")
End If
End Sub
Sub dbClose
Set ogConn = Nothing
If bgDebug Then
ogDebug.WriteLine("Close data base connection.")
ogDebug.Close
End If
End Sub
%>
Note that: All variables defined in _config.inc should be global variables so they become accessible in any functions in any ASP pages. A "Dim" statement is needed to make a variable global. Database connection process should be a configuration item, because it is always different from one Web server to another. I also included a debug flag in the configuration file to control printing debug information in a log file. Page Layout Templates In order for hyBook to be integrated into different into different Web site, where each of them may different page layout, I have designed hyBook page to be based on template include files. Here is how this design works. 1. Each ASP page will have a template include file inserted at the beginning. 2. The template include file provides code to control the page layout. 3. The page layout should have blank blocks where some ASP function calls will be placed. 4. Each ASP page should only implement those ASP functions called by the template. For example, the following is a template include file in my hyBook demonstration package: Code:
<% ' Template file ' hyBook version 2006.01.01 ' Copyright (c) 2006 by Dr. Herong Yang, http://www.herongyang.com/ opening %> <?xml version="1.0"?> <html><head> <title>hyBook - Demo</title> <meta name="description" content="Demonstration page of hyBook - A simple guest book script"/> <meta name="keywords" content="hyBook, Guestbook, ASP"> <link rel="stylesheet" type="text/css" href="hyBook.css"/> </head><body> <center> <% outputHeader %> <!-- Other HTML or ASP codes --> <% outputBody %> <!-- Other HTML or ASP codes --> <% outputFooter %> <p>Powered by <a href=http://www.herongyang.com/hyBook/>hyBook</a></p> </center> </body></html> <% closing %> Note that there are 5 ASP function calls placed in this template: "opening" - Giving the ASP page a chance to do initialization work, like establishing database connection, and checking input data in the HTTP request. "closing" - Giving the ASP page a chance to do closing up work, like closing database connection. "outputHeader" - Allowing the ASP page to generate HTML code designed as the header block. "outputBody" - Allowing the ASP page to generate HTML code designed as the body block. "outputFooter" - Allowing the ASP page to generate HTML code designed as the footer block. What's goes into the header block, the body block, and the footer block is up to the author of the page. Of course, if you don't use any of those blocks, just implement an empty function in the ASP page. Also note that a CSS file, hyBook.css, is designed in hyBook to allow you control the appearance of individual HTML elements. Guestbook Main Page (To be continued) Handling Data Submission Issues (To be continued) Webmaster Administration Page (To be continued) ------------ For the complete version of this tutorials, see http://www.herongyang.com/asp/hybook.html |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Writing Your Own Guestbook in ASP - Part I |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|