Home|Sitemap|Contact

How can you record page access to a database?

First, your web server software already has a web log.  For it to be useful you need access to the log files, which may not be available with certain ISP's.  You may also need log analysis software to make sense of the log entries.  Sometimes people want something simpler.  This simple example writes information about access to the page to a database using ASP and an OLEDB or ODBC database (Access, MS SQL, mySQL, Oracle, etc.)

<!--METADATA TYPE="typelib" uuid="00000205-0000-0010-8000-00AA006D2EA4" -->
<%
' This asp code should be saved to a stand alone include file; "accesslog.asp"
Option Explicit
Session.CodePage=65001
Response.Charset="UTF-8"

'****************************************************
'* Weblog database table definition                 *
'*--------------------------------------------------*
'* ID AutoNumber Primary Key                        *
'* username Text (200) Indexed Duplicates OK        *
'* accessdate DateTime                              *
'* httpheaders Memo                                 *
'****************************************************

Dim cmd, conn, RS, cmdText, param, numAffected

Set cmd = Server.CreateObject("ADODB.Command")
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open YOUR DATABASE CONNECTION STRING HERE

cmdText = "INSERT INTO weblog ( username, accessdate, httpheaders ) VALUES ( @username, @accessdate, @httpheaders );"

Set param = cmd.CreateParameter("username",adVarChar,adParamInput,CLng(200),request.servervariables("AUTH_USER"))
cmd.Parameters.Append param
Set param = cmd.CreateParameter("accessdate",adDate,adParamInput,CLng(16),NOW)
cmd.Parameters.Append param
Set param = cmd.CreateParameter("httpheaders",203,adParamInput,CLng(4096),Request.ServerVariables("ALL_HTTP"))
cmd.Parameters.Append param

cmd.commandText = cmdText
Set cmd.ActiveConnection = conn

cmd.Execute numAffected,,adExecuteNoRecords
%>

To write to the log, simply include the above file in your page's code.  The page must have an .asp extension.

<!--include file="accesslog.asp"-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
</head>
<body>
</body>
</html>


Most Popular Pages On rodsdot.com

Client Side Includes Using JavaScript
Creating A JavaScript Slider Input in JavaScript
Creating Conditional Links With ASP
Dynamically Adding Rows and Fields To A Table Based Form
Geo-Coding IP Addresses
How To Make A Pop Over Menu
JavaScript Date Validation DD/MM/YYYY
JavaScript Date Validation MM/DD/YYYY
Parameterized SQL Using Multiple Form Inputs and Filtering
Play A WAV File On Mouseover
Pop Over Form
Resorting A Multi-Dimensional Table Using Client-Side JavaScript
Scripting Remote Images in JavaScript
Using AJAX to Return HTML For Dynamic Forms
Using Images For CAPTCHA
Using Parameterized SQL with ASP and MS Access
Why JavaScript As The HREF Of A Link Is Bad