dbHitMe Site traffic logger

This is a simple traffic logger I tossed together in the start of my ASP coding days. It simply collects data from your visitors and stores it in MS Access 97 mdb file. Due to the srestrictions in simultaneous hits to a web site running MS Access as a database, this is only suitable for sites that have less than 20 simultaneous hits.

I am working on an executable replacement for this, that will enable you to have up to 255 hits simultaneously instead.

Place the following information at the top most position in the file you wish to count/log.

<!-- #INCLUDE FILE="cgi-bin/dbHit.asp" -->
response.expires = 0
response.expiresabsolute = Now() -1
response.addHeader "pragma","no-cache"
response.addHeader "cache-control","private"
Response.CacheControl = "no-cache"
<!--call the function-->

The segment about response.expires and so on is simply included due to a server side caching dilemma in IIS 4 and 5. If you dont include this on IIS5 / W2K servers, the server may end up caching your documents and your pages doesnt refresh until the server refreshes it's own cache files. A most annoying fact when being in the middle of an update and reloading a lot of pages for testing by ftp.

Below is the ASP code for dbHit.asp

Function HITME()
HITME = " <--HitCounter by MRJ Design--> "

' Defining some constants to make my life easier!
' Begin Constant Definition
' DB Configuration constants
' Fake const so we can use the MapPath to make it relative.
' After this, strictly used as if it were a Const.


DB_CONNECTIONSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
Server.Mappath("cgi-bin/dbHitlog.mdb") & ";"

' ADODB Constants

'should be below the next asp delimiter but moved it to the head file for speed

Dim I            ' Standard looping var
Dim strSQL       ' String variable for building our query
Dim iRecordAdded ' Id of added record

'We're going to keep this as simple as we can.
'  1. Create a Recordset object
'  2. Connect the Recordset to the table
'  3. Add a new record to the Recordset
'  4. Set the values of the Recordset
'  5. Update the table
'  6. Close the Recordset

'Step 1:
Dim objRecordset
Set objRecordset = Server.CreateObject("ADODB.Recordset")

' The following syntax is also acceptable if you move it outside of the
' script delimiters.  I prefer to Dim and then set it like any other
' variable, but it really doesn't make too big a difference.

'<OBJECT RUNAT=server PROGID=ADODB.Recordset ID=objRecordset></OBJECT>

'Step 2:
strSQL = "SELECT * FROM hitlog WHERE ID <> 0;"
' This is the way I normally would open this RS:
'objRecordset.Open strSQL, DB_CONNECTIONSTRING, adOpenKeyset, adLockPessimistic, adCmdText
objRecordset.Open strSQL, DB_CONNECTIONSTRING, 1, 2, &H0001

'Step 3:
' AddNew Method.
'Step 4:
' Date / Time Data Type
objRecordset.Fields("DATE") = Now()
objRecordset.Fields("HTTP_USER_AGENT") = Request.ServerVariables("HTTP_USER_AGENT")
objRecordset.Fields("HTTP_REFERER") = Request.ServerVariables("HTTP_REFERER")
objRecordset.Fields("QUERY_STRING") = Request.ServerVariables("QUERY_STRING")
objRecordset.Fields("REMOTE_ADDR") = Request.ServerVariables("REMOTE_ADDR")
objRecordset.Fields("REMOTE_HOST") = Request.ServerVariables("REMOTE_HOST")
objRecordset.Fields("SERVER_PORT") = Request.ServerVariables("SERVER_PORT")
objRecordset.Fields("URL") = Request.ServerVariables("URL")

'Step 5:

'Show the user something:
' Get the DB assigned ID of the record we just added.
iRecordAdded = objRecordset.Fields("ID").Value

' Tell people which record we added.
Response.Write "&ltfont size=2 color=#FFFFFF face='verdana,arial'>Visitors since June 26, 2001 #" & iRecordAdded & "<font>"

'Step 6:
' Finally we close the recordset and release the memory used by the
' object variable by setting it to Nothing (a VBScript keyword)
Set objRecordset = Nothing

End Function

You might also like...


Mike J


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie