Library code snippets

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

Comments

  1. 16 Dec 2004 at 21:36
    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
  2. 17 Oct 2003 at 11:40

    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! I know the problem resides in IE6's privacy settings and the code will work if I tell IE to allow cookies for the particular concerned domain.


    My question is, does anyone know a fix for this code so it will work with IE6?


    Regs,
    gadago

  3. 14 Oct 2002 at 13:37
    Great!
    To me, this code is working fine
    Thanx James.
  4. 13 Jul 2002 at 14:49

    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

  5. 12 May 2002 at 17:06

    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


    click here to visit my site - I can also be reach via online help chat box.


    Thank you very - very much!


    -Jack

  6. 05 Apr 2002 at 00:50
    Excuse me, but the file global.asa is on my root web and i have placed There are currently <%=Application("Active")%> World Girls online into the home.htm page. It displays as There are currently World Girls online. Why?
  7. 14 Mar 2002 at 03:42
    I hope you are still online ! I have created a global.asa file on c:\inetpub\wwwroot\global.asa and one on d:\somewhere\icafe\global.asa
    I have included as you can see on http:\\icafe.talente.ro the code into the HTML file which point to the asa file I believe.
    I RECEIVE

    Server Application Error
    The server has encountered an error while loading an application during the processing of your request. Please refer to the event log for more detail information. Please contact the server administrator for assistance

    The server failed to load application .  The error was 'Server execution failed
    '.
    For additional information specific to this message please visit the Microsoft Online Support site located at: http://www.microsoft.com/contentredirect.asp.

    The server did not register with DCOM within the required timeout.
  8. 14 Mar 2002 at 02:49
    nope its not as far as i know. create any old file so right click on explorer and click new textfile and just call it Global.asa, make sure its in the root directory... so if your website is in the folder C:\Inetpub\wwwroot\ then thats where the file should be created.

    as for default.htm i think its best you change all your extensions to .ASP for future convienence and for ASP scripting.
  9. 14 Mar 2002 at 02:03
    Global.asa should be pre-created (installed) or I should do that ? i am using Windows 2000 server and I have configured few subdomains like icafe.talente.ro on my domain www.talente.ro. My default page in every site is default.htm This asp code will work ? Should I change the default page for every site ?

    Thank you
  10. 13 Mar 2002 at 11:54
    Place the code in a global.asa file in the root of your IIS Web Application.
  11. 13 Mar 2002 at 11:08
    what operating system are you using? Windows 2000 Pro/AS or Windows XP?

    Windows 2000
    goto Start>Settings>Control Panel>Add/Remove Programs> Add/Remove Windows Components

    Windows XP
    Start>ControlPanel>Add/Remove Programs> Add/Remove Windows Components

    then simply tick the IIS checbox
  12. 13 Mar 2002 at 10:35
    Please help me to instal under IIS 5 the code to see the total number of visitors !
  13. 06 Mar 2002 at 11:10

    James You are correct


    thank you


    after the Session.Timeout occurs from the Sub Session_OnStart the number of users are reduced

  14. 06 Mar 2002 at 10:59

    How long have you waited for the count to go down? It depends on the length of the session... I think it's something like 30 mins before the session expires.

  15. 06 Mar 2002 at 10:13

    I have the opposite problem, when I use your code instead of the code not incrementing, it wont decrement.



    I tested your code by creating the asa in the same diresctory as my asp page


    then when i open internet explorer with my asp page it displays the number of users as 1..
    (good so far)



    then when a different computer opens internet explorer with my asp page it displays the number of users as 2..
    (also good)



    then when one of the Computers shuts down Internet Explorer the other computer still displays two users present even after numerous refresh rates..
    (where i have problems)



    ..Que
    Does the session not end when the Web browser closes?
    Do I have togo through IISManager right click the directory the asa is in click on properties and create an application for the code to work properly?


    Thank You..

  16. 22 Feb 2002 at 16:41
    hey,

    no worries james... i fixed up the issue.
  17. 22 Feb 2002 at 07:24

    My apologies... some of the variables had incorrect names. I've corrected it now.

  18. 10 Jan 2002 at 06:32

    I cant get this to work on IIS 5.1(WinXP Pro) its just stays as zero although I hooked up 3 LAN PC's to visit the page any ideas?

  19. 01 Jan 1999 at 00:00

    This thread is for discussions of Number of visitors online.

Leave a comment

Sign in or Join us (it's free).

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 ...

Want to stay in touch with what's going on? Follow us on twitter!