Number of visitors online

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

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor