Ever wondered how sites display the number of visitors currently using the site? It's actually quite simple in ASP. First, add the following code to a global.asa file
<script language="vbscript" runat = "server">
Sub Application_OnStart
'initialize variable
Application("visitors_online") = 0
End Sub
Sub Application_OnEnd
End Sub
Sub Session_OnStart
Session.Timeout = 20 '20 minute timeout
Application.Lock
Application("visitors_online") = Application("visitors_online")
+ 1
Application.Unlock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors_online") = Application("visitors_online")
- 1
Application.Unlock
End Sub
</script>
This script is run by IIS. When a session starts, it calls Session_OnStart
,
and when it ends, it calls Session_OnEnd
, allowing you to keep track
of the number of active visitors. You can then display the number of visitors
online by using
There are currently <%=Application("visitors_online")%> visitor(s)
online
Comments