View Full Version : See the Working Counter Example!
anandc
10-06-2002, 08:32 PM
hi everybody
i m attaching a counter example which i m successfully running
on my site's (http://www.bhandaradist.com) home page.
Here i have used File Object. I have tested it with WinNT 4.0 also Win 98 SE and PWS. I have never encountered problem of Permission Denied or any other.
Hope it will help you a lot.
Have a nice day
Anand
whammy
10-06-2002, 09:20 PM
Not bad! :)
Here's my "Easy Counter"... it is very similar, but has a couple of additional features:
It maps a path to a folder above your root directory (useful for brinkster.com users)
Uses a session variable to prevent incrementing the counter variable by refreshing
Creates the text file for you if it doesn't exist
;)
You can copy and paste the script from here:
http://www.solidscripts.com/solidscripts_new/displayscript.asp?sid=4
biggs27
10-21-2002, 04:54 PM
In the spirit of sharing counters, I though I would post a pretty simple ASP counter I wrote some time ago. This code works with an Access Database and tracks hits to page and IP address by the URL of each page. The second part (Red text within HTML tags) will display a count on the page itself. A warning on that though, after a large number of hits, your page will lose some performance because the count is performed every time the page is loaded.
I wouldn't expect anyone to use this verbatim, but maybe it'll spark some ideas.
<!-- Make a connection to the database, select the table for input.
Once this is correct, the code will work on any page. The datebase
needs only one table (here named tblHome) with fields
"Hits", "Date", "Time", "Path", and "RemAddress". -->
<%
Dim SQL, rs, dbCalendar, cnCalendar, sMDB, Conn, strSQL
sMDB = ("Your Database Location.mdb")
set Conn = server.createobject("adodb.connection")
Conn.open "driver={Microsoft Access Driver (*.mdb)};dbq=" & sMDB & ";uid=Admin"
SQL = "SELECT * FROM tblHome"
Set rs = Server.CreateObject("adodb.recordset")
rs.Open SQL, Conn, 3, 3
%>
<!-- Select the information that you want to send to the database. "Variable2" and "Hits" MUST be included
for the counter to function. The field names give
above correspond. -->
<%
rs.AddNew
rs("Hits")= 1
rs("Date") = Date()
rs("Time") = Time()
rs("Path") = Request.ServerVariables("PATH_INFO")
rs("RemAddress") = Request.ServerVariables("REMOTE_ADDR")
rs.Update
rs.Close
%>
<html>
<body>
<!-- Output at the bottom of the page. This query counts the hits based on the location of this page. -->
<%
strSQL = ("SELECT SUM (Hits) AS CountofHits from tblHome WHERE Path = '")& Request.ServerVariables ("PATH_INFO")&"'"
Set RS = Conn.Execute(strSQL)
%>
<p>This page has been hit <%=rs("CountofHits")%> times. </p>
</body>
</html>
whammy
10-23-2002, 12:19 AM
Ok, I'm going to one up you on this too. ;)
Here's what I'm currently using to track page visits... it uses an Access database (although you could just as easily use SQL Server), and tracks ip,useragent,referrer,pagevisited,and hittime:
counter.asp:
<%
Dim Conn,sConnString,sMapPath,INSERTQUERY,GETIDQUERY,rs
Dim id,ip,useragent,referrer,pagevisited,hittime
If session("counted") <> Request.ServerVariables("PATH_INFO") AND Request.ServerVariables("REMOTE_ADDR") <> "12.345.678.90" Then 'Replace IP here with yours!
ip = Request.ServerVariables("REMOTE_ADDR")
useragent = Request.ServerVariables("HTTP_USER_AGENT")
referrer = Request.ServerVariables("HTTP_REFERER")
If referrer = "" Then referrer = "None"
pagevisited = Request.ServerVariables("PATH_INFO")
If Request.ServerVariables("QUERY_STRING") <> "" Then
pagevisited = pagevisited & "?" & Request.ServerVariables("QUERY_STRING")
End If
hittime = Now()
Set Conn = Server.CreateObject("ADODB.Connection")
sConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
sMapPath = Server.MapPath("\")
sMapPath = Mid(sMapPath, 1, InStrRev(sMapPath,"\")-1) & "\database\scripts.mdb;" & _
"Persist Security Info=False;"
sConnString = sConnString & sMapPath
Conn.Open sConnString
INSERTQUERY = "INSERT INTO hitcounter (ip,useragent,referrer,pagevisited,javascriptenabled,hittime) VALUES ('" & ip & "','" & useragent & "','" & referrer & "','" & pagevisited & "',0,'" & hittime & "')"
Conn.Execute(INSERTQUERY)
GETIDQUERY = "SELECT id FROM hitcounter WHERE ip = '" & ip & "' AND hittime = #" & hittime & "#"
Set rs = Conn.Execute(GETIDQUERY)
If NOT rs.EOF Then
id = rs("id")
End If
Conn.Close
Set Conn = Nothing
GetJavaScript() 'Find out if they have javascript enabled!
session("counted") = Request.ServerVariables("PATH_INFO")
End If
%>
<% Sub GetJavaScript() %>
<script type="text/javascript">
<!--
document.write('<img src="http://www.solidscripts.com/includes/javascriptenabled.asp?id=<% = id %>&js=1" width="1" height="1" />');
// -->
</script>
<% End Sub %>
javascriptenabled.asp (you don't have to include this file it's called by the javascript trick!):
<%
id = Request.QueryString("id")
js = Request.QueryString("js")
If id <> "" AND js = "1" Then
Set Conn = Server.CreateObject("ADODB.Connection")
sConnString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
sMapPath = Server.MapPath("\")
sMapPath = Mid(sMapPath, 1, InStrRev(sMapPath,"\")-1) & "\database\scripts.mdb;" & _
"Persist Security Info=False;"
sConnString = sConnString & sMapPath
Conn.Open sConnString
UPDATEQUERY = "UPDATE hitcounter SET javascriptenabled = 1 WHERE id = " & id
Conn.Execute(UPDATEQUERY)
Conn.Close
Set Conn = Nothing
End If
%>
Of course you will have to change the connection around to suit your server (I know the one above is rather lengthy, but I've had problems trying to use anything else with my host for some reason :|). But this tracks pretty much all I care to know, simply by including "counter.asp" in any page I want to track, including whether the visitor has javascript enabled.
P.S. javascriptenabled is a "bit" field. The rest should be self-explanatory... mostly text, id is autoincrement, and datetime is a date/time field.
I also changed the above script (since I'm currently tracking only one page) to use Server.RequestVariables("PATH_INFO") to track the page in question only once per session.
:D
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.