Hello
I have a counter, that is, a couple of files (example.asp and hit_counter.inc), plus a mdb on my server.
The important example.asp code is:
<div align="center">
This page has been accessed<br />
<%=GetCurrentHitCount("example", true)%><br />
times
</div>
while in the hit_counter.inc, I have:
<%
'This function returns an open connection to the access database.
Function GetDBConnection()
Dim databasePath, connectionString
'MODIFY: Specify the relative path to the database file.
databasePath = ".\HitCounter.mdb"
'Connect to the Access database using a DSN-Less connection
Set GetDBConnection = Server.CreateObject("ADODB.Connection")
connectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath(databasePath)
GetDBConnection.Open connectionString
End Function
'This function will return the current hit count for the page specified by pageName.
' If incrementHitCount (boolean) is true, the it will first add one to the current hit count.
Function GetCurrentHitCount(pageName, incrementHitCount)
Dim con, rs, sql, currentHitCount
'Get the database connection
Set con = GetDBConnection()
'Get the current hit count
Set rs = Server.CreateObject("ADODB.Recordset")
sql = "SELECT * FROM tblHitCount WHERE PageName = '" + pageName + "'"
rs.Open sql, con, 2, 3
If (rs.EOF) Then
'No count was found, create a new record and start the count at 0
rs.AddNew()
rs("PageName") = pageName
rs("CurrentHitCount") = 0
rs.Update()
End If
'Get the current hit count from the database
currentHitCount = rs("CurrentHitCount")
If (incrementHitCount) Then
'Add one to the current hit count before retrieving it
currentHitCount = currentHitCount + 1
rs("CurrentHitCount") = currentHitCount
rs("LastAccessed") = Date()
rs.Update()
End If
'return the hit count
GetCurrentHitCount = currentHitCount
'Clean up database objects
rs.Close()
con.Close()
Set rs = Nothing
Set con = Nothing
End Function
%>
They both work fine. However, when I paste the script from example.asp, that is,
<div align="center">
This page has been accessed<br />
<%=GetCurrentHitCount("example", true)%><br />
times
</div>
into my own home page, I cannot see the number of 'hits' (it is blank).
The problem may be related to the database connection, yet this was not a problem when example.asp and hit_counter.inc worked together - the problem occurs only when I copy and paste the script from example.asp into my own page.
All files (my own home pages and the above example.asp, hit_counter.inc and mdb files are all on the server in one directory.
Any ideas, please?
Thanks.
Steve
I have a counter, that is, a couple of files (example.asp and hit_counter.inc), plus a mdb on my server.
The important example.asp code is:
<div align="center">
This page has been accessed<br />
<%=GetCurrentHitCount("example", true)%><br />
times
</div>
while in the hit_counter.inc, I have:
<%
'This function returns an open connection to the access database.
Function GetDBConnection()
Dim databasePath, connectionString
'MODIFY: Specify the relative path to the database file.
databasePath = ".\HitCounter.mdb"
'Connect to the Access database using a DSN-Less connection
Set GetDBConnection = Server.CreateObject("ADODB.Connection")
connectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath(databasePath)
GetDBConnection.Open connectionString
End Function
'This function will return the current hit count for the page specified by pageName.
' If incrementHitCount (boolean) is true, the it will first add one to the current hit count.
Function GetCurrentHitCount(pageName, incrementHitCount)
Dim con, rs, sql, currentHitCount
'Get the database connection
Set con = GetDBConnection()
'Get the current hit count
Set rs = Server.CreateObject("ADODB.Recordset")
sql = "SELECT * FROM tblHitCount WHERE PageName = '" + pageName + "'"
rs.Open sql, con, 2, 3
If (rs.EOF) Then
'No count was found, create a new record and start the count at 0
rs.AddNew()
rs("PageName") = pageName
rs("CurrentHitCount") = 0
rs.Update()
End If
'Get the current hit count from the database
currentHitCount = rs("CurrentHitCount")
If (incrementHitCount) Then
'Add one to the current hit count before retrieving it
currentHitCount = currentHitCount + 1
rs("CurrentHitCount") = currentHitCount
rs("LastAccessed") = Date()
rs.Update()
End If
'return the hit count
GetCurrentHitCount = currentHitCount
'Clean up database objects
rs.Close()
con.Close()
Set rs = Nothing
Set con = Nothing
End Function
%>
They both work fine. However, when I paste the script from example.asp, that is,
<div align="center">
This page has been accessed<br />
<%=GetCurrentHitCount("example", true)%><br />
times
</div>
into my own home page, I cannot see the number of 'hits' (it is blank).
The problem may be related to the database connection, yet this was not a problem when example.asp and hit_counter.inc worked together - the problem occurs only when I copy and paste the script from example.asp into my own page.
All files (my own home pages and the above example.asp, hit_counter.inc and mdb files are all on the server in one directory.
Any ideas, please?
Thanks.
Steve