The Front Page Wizard

 Home | ASP Tips | DHTML Tips | JavaScript Tips | Alt Keys | Contact The Wizard |

 

 

 

 

We Recommend FrontLook

Products for Microsoft® FrontPage®

Get yours Today - Click Here

 

 

Support This Site

Help To Support

Our Site - Get Some

Wizard Gear

 

 

Help Us Keep

The Site Alive

 

 

 

 

 

Visitors Online Script

 

This is a J-Bots Component!

The following ASP Tip comes from our friends at : : : n a r s o ' s   p r o g r a m m i n g   d e n
Visit him on the web at: ::: n a r s o ' s   p r o g r a m m i n g   d e n

This is a mini tutorial on how to count the number of active visitors accessing your site at the moment. first, you need to have a file named "global.asa" in the base directory of your site. this file is used by your application to do operations during the OnStart and OnEnd events for the Application and Session objects.

 

Paste the code below to the said file:
 

Note: Copy the script below into a blank notepad page and save it as a text file. Then recopy into your code from it.


<script language="VBScript" runat="Server">
Sub Application_OnStart
application("activevisitors") = 0
End Sub

Sub Application_OnEnd
End Sub

Sub Session_OnStart
application.lock
application("activevisitors") = application("activevisitors") + 1
application.unlock
End Sub

Sub Session_OnEnd
application.lock
application("activevisitors") = application("activevisitors") - 1
application.unlock
End Sub
</script>


Here's the explanation of the code:


The script tag at the beginning of the code is used to tell the asp engine what language is used and where the script should be executed. it is important to set the "runat" property with the value "server".


The four sub procedures are called event handlers. when an event occurs, its appropriate event handler is invoked. in active server pages, the four events that need attention are the start (Application_OnStart) and end (Application_OnEnd) of the application and the start (Session_OnStart) and end (Session_OnEnd) of each asp session.


The Application_OnStart event is fired when the first session requests an .asp file from your application directory since the IIS last started or the application timed-out. for this event, we initialize our active visitors counter to zero. again the application object here exposes its another useful feature. the operation application("activevisitors") = 0 creates a variable named activevisitors in the application object. therefore, you can also think of the application object as an object with a list of custom variables created by your application. you create a variable and access its value by using the following syntax:
application("variablename").


On the other hand, the Application_OnEnd event is fired when all sessions of your application time-out. since we don't need to do anything in this event, the procedure body is empty.


The Session_OnStart event is fired when a user, by using their browser, request an .asp file from your application directory either for the first time, or after a previous session with the client browser has timed-out or has been abandoned. for this event, it is obvious that we need to increment our active visitors counter. to do this, we just can't directly invoke application("activevisitors") = application("activevisitors") + 1. we need to first call application.lock. this locking feature is used to simply ensure that no other user session simultaneously attempts to update the application object properties. such feature is only available in the application object and not in the session object. after the locking procedure, you can update any variable or property. after the property updates you must invoke the application.unlock method to release the lock so that other sessions can update if necessary.


Opposite the OnStart event above is the Session_OnEnd event, which is fired when a user's session time-out property value exceeds the number of minutes allowed since the last page request, or the session.abandon method is invoked by your application's code. for this event, we decrement the active visitors counter since the session has already ended. similar to the Session_OnStart procedure, we need to lock, decrement the counter variable and then unlock the application object.


Since we've already set up our event handlers, we can now go and add our counter to our .asp page. in the body of your .asp page, simply add the following code:

 

Copy the script below into a blank notepad page and save it as a text file. Then recopy into your code from it. Make sure this code is in the < Body > section and its on a .asp page.... :-)

 

There are currently <%=application("activevisitors")%> active visitors.

 


See how simple it is in retrieving the value of your active visitors counter stored in the application object. now, every time a visitor comes to your .asp application / homepage you can see the actual number of visitors in your page. also, you can extend the code above to use images of numbers as your counter display. maybe that will be a future tutorial. well, that's all for now. hope i somehow enlightened your mind on this subject. chow!

 

Written by n.mondejar III    Many Thanks for his work!
e-mail comments & suggestions to nars17@hotmail.com

 

 

 

 

 

Copyright © 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007   FrontPageWizard.com    All Rights Reserved

Microsoft® and FrontPage® are registered trademarks of Microsoft Corporation.
Microsoft Corporation is in no way affiliated with nor dose it endorse FrontPageWizard.com

FrontPage Help Center By: Karey Cummins of Round The Bend Wizards  •  

Curser by: Lisa's Curser  •  Meta Tag Generator by: SearchBliss   •  RGB Color Calculator by: Greg Reimer