Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 27,471 times

Related Categories

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

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • help me

    Posted by kokkee on 16 Dec 2004

    help me pls, why i copy all the code alerady, but still no display the number of visitors....
    pls teach me how to use it... thanks a lot

  • IE6 Incompatability

    Posted by gadago on 17 Oct 2003

    This code works fine for me. However if I view that page under IE6 this deosnt work correctly. It will display double the amount of users that are online evertime the page is refreshed, which is false...

  • It works fine.

    Posted by paford on 14 Oct 2002

    Great!
    To me, this code is working fine :)
    Thanx James.

  • ???

    Posted by greacy on 13 Jul 2002

    It don't work cos you haven't placed the code on a asp page! If you sever supports it save your homepage as home.asp

  • Help

    Posted by NastyJack on 12 May 2002

    I need some help with this script...

    If anyone has the time to help me with it please contact me via e-mail at nastyjack@rogers.com

    Date Of This Post May/12/02

    [url="http://www.triplexfree.c...