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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple ASP hit counter

Status
Not open for further replies.

pixel69

Technical User
Sep 25, 2002
56
ZA
I'm looking for a simple easy to implement ASP hit counter for a website!! Please assist!!

pixel
 
This uses global.asa file

<script language=vbscript runat=server>
Sub Application_OnStart
Application(&quot;visitors&quot;)=0
End Sub

Sub Session_OnStart
Application.Lock
Application(&quot;visitors&quot;)=Application(&quot;visitors&quot;)+1
Application.Unlock
End Sub
Sub Session_OnEnd
Application.Lock
Application(&quot;visitors&quot;)=Application(&quot;visitors&quot;)-1
Application.Unlock
End Sub
</script>

And in any asp page you can do

Response.Write &quot;Visitor nr. &quot;& Application(&quot;visitors&quot;)

________
George, M
 
Where does this write the hits to? And how does one reset it??
 
You can store to a database or to a file. Database should be better.

<script language=vbscript runat=server>
Sub Application_OnStart
'open connection to database
set rs=con.Execute(&quot;select visitors from visitorsDB&quot;)
Application(&quot;visitors&quot;)=rs(&quot;visitors&quot;)
End Sub

Sub Application_OnEnd
'open connection to database
set rs=con.Execute(&quot;update visitorsDB set visitors=&quot;&Application(&quot;visitors&quot;))
End Sub

Sub Session_OnStart
Application.Lock
Application(&quot;visitors&quot;)=Application(&quot;visitors&quot;)+1
Application.Unlock
End Sub
Sub Session_OnEnd
Application.Lock
Application(&quot;visitors&quot;)=Application(&quot;visitors&quot;)-1
Application.Unlock
End Sub
</script>

Session mofdify the Application(&quot;visitors&quot;) wich is a global variable. Application stores global variables.
It is initialized when web server starts and close down when it stops.
When it starts i get the old counter from database and when it end i'll store to it.
When a session starts(an user came to your web site) it will increase the visitor number and when it leaves it will decrease it.

________
George, M
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top