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
Related articles
Related discussion
-
Binary Studio | software development outsourcing Ukraine
by Soft Industry (5 replies)
-
asp Request.QueryString("dir")
by realmeteo (1 replies)
-
Looking for Senior Web Designer
by lwsmedia (0 replies)
-
Read eMails from Outlook express using ASP
by kumaravelu (1 replies)
-
Help to Call ASP function from onclick event in HTML to pass an array
by vka (0 replies)
Related podcasts
-
Scott Guthrie
Scott catches up with Scott Guthrie in an interview covering Ajax, Asp 2.0, extender controls, CSS adapters and more.
pls teach me how to use it... thanks a lot
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
To me, this code is working fine
Thanx James.
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
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
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.
as for default.htm i think its best you change all your extensions to .ASP for future convienence and for ASP scripting.
Thank you
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
James You are correct
thank you
after the Session.Timeout occurs from the Sub Session_OnStart the number of users are reduced
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.
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..
no worries james... i fixed up the issue.
My apologies... some of the variables had incorrect names. I've corrected it now.
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?
This thread is for discussions of Number of visitors online.