ASP Source Code: - hitCount.inc
<%
dim VisitorCount
sub hitCount(PageID)
'Get Page Hit Count
'Table Name = Counter
'Table Fields = Sequence (Autonumber key), ID, Count
dim tbl
dim flag
dim conn
dim rs
dim strSQL
dim strCookie
tbl = "Counter"
flag = "old"
strCookie = replace(PageID,".","_")
'Note - Session variables could be used instead of cookies
VisitorCount = Request.Cookies(strCookie)("value")
If VisitorCount = "" then
Set conn=Server.CreateObject("ADODB.Connection")
conn.Open mulveyDSN
' Set rs = Server.CreateObject("ADODB.Recordset")
' rs.open tbl,conn,adOpenKeyset,adLockOptimistic
strSQL="Select CurCount from Counter where ID='" & PageID & "';"
Set rs=conn.Execute(strSQL)
if not rs.BOF then
VisitorCount = rs("CurCount")
flag = "old"
else
'This page has never been visited before
VisitorCount = 0
flag = "new"
end if
rs.Close
VisitorCount = VisitorCount + 1
Response.Cookies(strCookie)("value")=VisitorCount
Response.Cookies(strCookie)("HttpOnly")=true
Response.Cookies(strCookie)("Secure")=true
if flag = "old" then
'Update the counter record
strSQL = "update " & tbl & " set CurCount=" & VisitorCount & " where ID='" & PageID & "';"
conn.execute(strSQL)
else
'Create a record for a page which is visited for the first time
rs.open tbl,conn,adOpenKeyset,adLockOptimistic
rs.AddNew
rs("ID") = PageID
rs("CurCount") = 1
rs.Update
end if
set rs = nothing
set conn = nothing
end if
end sub
%>