Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SIMPLE ASP PAGE HIT COUNTER ?

Status
Not open for further replies.

rockyroad

Programmer
Feb 22, 2003
191
US
Hello,

Does anyone know a simple ASP script that will
serve as a hit-counter, without relying on a database to store the hit values?

I am inexperienced with ASP, as I am a ColdFusion developer.
However, I have been volunteering my time as the web designer/developer for a non-profit,

If anyone can help out with a simple solution, the organization and I would be forver grateful. :)

Thanks,

C Bataglio (RR)
 
>> without relying on a database to store the hit values

So that leaves a file is suppose. You can use the FileSystemObject to read and write to files but I don't know how concurrency is going to effect it.

-pete


 
Thanks, Pete, but I am not sure I understand your
reply. I am <i>really</i> inexperienced with ASP! :-0

thanks

CB
 
Use the &quot;keyword search&quot; for this forum using the keyword FileSystemObject

-pete

 
Pete congrats to you on being No 1 for the week.

CB
A simple text file could be a problem on a busy site.

If you want something that can hit count every (asp) page on your site, this simple ASP script it uses an Access db called count.mdb, 1 table called 'counter' in a /tables/ folder, db structure marked in the comments. To use, put this code in an included file and put
<%
hitCount(&quot;pagename&quot;)
%>
on any page whenever you need to use it,
It stores the PageID in a session variable so each access will only get counted once, refresh doesn't add neither does a page revisit (back or navigated)The routine will leave <%VisitorCount%> containing the current count for the page.

<%
Const adOpenKeyset = 1
Const adLockOptimistic = 3
dim VisitorCount
sub hitCount(PageID)
'Get Page Hit Count
'Table Name = Counter
'Access Table Fields = Sequence (Autonumber key), ID Text 250, Count Number Long Integer

dim tbl
dim flag
dim conn
dim rs
dim strSQL

tbl = &quot;Counter&quot;
flag = &quot;old&quot;

VisitorCount = Session(PageID)
If VisitorCount = &quot;&quot; then
Set conn=Server.CreateObject(&quot;ADODB.Connection&quot;)
conn.Open &quot;DRIVER={Microsoft Access Driver (*.mdb)};DBQ=&quot; & Server.mapPath(&quot;/tables/count.mdb&quot;) & &quot;;&quot;
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.open tbl,conn,adOpenKeyset,adLockOptimistic
strSQL=&quot;Select Count from Counter where ID='&quot; & PageID & &quot;';&quot;
Set rs=conn.Execute(strSQL)
if not rs.BOF then
VisitorCount = rs(&quot;Count&quot;)
flag = &quot;old&quot;
else
'This page has never been visited before
VisitorCount = 0
flag = &quot;new&quot;
end if
rs.Close

VisitorCount = VisitorCount + 1
Session(PageID) = VisitorCount
if flag = &quot;old&quot; then
'Update the counter record
strSQL = &quot;update &quot; & tbl & &quot; set Count=&quot; & VisitorCount & &quot; where ID='&quot; & PageID & &quot;';&quot;
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(&quot;ID&quot;) = PageID
rs(&quot;Count&quot;) = 1
rs.Update
end if
set rs = nothing
set conn = nothing
end if
end sub
%>

I will also put the script and table into a zip file for download if you wish.


Chris.



Indifference will be the downfall of mankind, but who cares?
 
Thank You very much for your help.

One question, wont Access become strained when the count gets
very high; i.e., they would like me to set the count to the number of hits the home page has gotten since I redesigned the site. The number is in the tens of thousands. I can easily write a little loop construct to populate a db table, but I am curious to the results in terms of Access being able to handle that many records. Any thoughts?

I really appreciate everyone's help here, thank you.

CB
 
Try the following! This shld work. BUT noted that all count will be reset once you restart your server.
Copy and paste the following into your global.asa file.

<SCRIPT LANGUAGE=&quot;VBScript&quot; RUNAT=&quot;Server&quot;>
Sub Application_OnStart
application(&quot;visitors&quot;)=0
End Sub

Sub Application_OnEnd
End Sub

Sub Session_OnStart
application.lock
application(&quot;visitors&quot;)=application(&quot;visitors&quot;)+1
application.unlock
End Sub


</SCRIPT>

Then put the following code into your asp file to display the number of visitor.

There are <% =application(&quot;activevisitors&quot;) %> active visitors.

Hope this help.
 
One problem, that application variable will reset everytime your server is reset, losing your total hit count. Here is an example that was posted a while back in the forums, I tried to find the thread but had difficulties. It opens a file, reads the number, increments that number, and writes it back to the file.
Code:
dim fs,f,ts

set fs=Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set f=fs.GetFile(Server.MapPath(&quot;counter.txt&quot;))

Set ts=f.OpenAsTextStream(1)	'open for read
Dim ctr
ctr = cInt(ts.ReadAll)

Response.Write &quot;Read: &quot; & ctr & &quot;<br>&quot;

ctr = ctr + 1
ts.Close

Response.write &quot;Writing &quot; & ctr & &quot;<br>&quot;

Set ts=f.OpenAsTextStream(2)	'open for write
ts.write(ctr)
ts.Close
set ts=nothing
set f=nothing
set fs=nothing

As was mentioned above, concurency could be an issue. To get around this what you can do is combine the two scripts.

In your global.asa file you could load the number of hits from this file as the initial number:
Code:
Sub Application_OnStart
   Dim fso, strm
   Set fso = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
   Set strm = fso.OpenTextFile(Server.MapPath(&quot;counter.txt&quot;),1)
   application(&quot;visitors&quot;)= cDbl(strm.ReadAll)
   strm.Close
   Set strm = Nothing
   Set fso = Nothing
End Sub

Then you could still incrememnt the counter in your global.asa Session_OnStart file and write that value to the file:
Code:
Sub SessionOnStart
   Dim fso, strm
   Set fso = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
   Set strm = fso.OpenTextFile(Server.MapPath(&quot;counter.txt&quot;),2)

   Application.Lock
   Application(&quot;visitors&quot;) = Application(&quot;visitors&quot;) + 1
   strm.Write Application(&quot;visitors&quot;)
   strm.Close
   Application.Close

   Set strm = Nothing
   Set fso = Nothing
End Sub

Now every time a visitor comes to your site they are counted and if the server is reset at any point then the number of hits will not disappear. Plus you can still write the number of hits without having to reopen your textfile, because it is still stored in the application variable.

-Tarwn

01010100 01101001 01100101 01110010 01101110 01101111 01101011 00101110 01100011 01101111 01101101
29 3K 10 3D 3L 3J 3K 10 32 35 10 3E 39 33 35 10 3K 3F 10 38 31 3M 35 10 36 3I 35 35 10 3K 39 3D 35 10 1Q 19
Get better results for your questions: faq333-2924
Frequently Asked ASP Questions: faq333-3048
 
another simple solution not to lose count even if the server restarted will be :

<SCRIPT LANGUAGE=&quot;VBScript&quot; RUNAT=&quot;Server&quot;>
Sub Application_OnStart
' getCountVisitors() function will get number of visitors from any database.
count = getCountVisitor()
application(&quot;visitors&quot;)=count
End Sub

Sub Application_OnEnd
' updateCountVisitors() function will go to update the number of visitors everytime the server get restarted
updateCountVisitors() = application(&quot;visitors&quot;)
End Sub

Sub Session_OnStart
application.lock
application(&quot;visitors&quot;)=application(&quot;visitors&quot;)+1
application.unlock
End Sub


</SCRIPT>

Hope this help.
 
In all of these solutions using the FileSystemObject what accounts for concurrency? Is it built into FSO? That does not seem likely.

If not there will likely be undesirable results should the site incur heavy load periods.

Even if the FSO does provides synchronization then this would produce a scaling issue during heavy load. In either case it is a sub optimal solution compared to using a database.

-pete



 
palbano, my solution that i provided shld be able to deal with your concern. it is because my way will only update the count using the application object. It will only access database when we start or restart the server as acessing and updating database only happen in Sub Application_OnStart and Sub Application_OnEnd. refer to the example i posted previously.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top