ASP & IIS: global.asa and current user count
The first time I've heard about the global.asa file, it was a long time ago when I wanted to show the current users on a home page. In fact, using the global.asa is not that difficult, and showing the users online is an old trick. I would like to show you what global.asa is and then use it to create a users online badge.
There must be only one global.asa in the root of your web site and it can contain only:
The ASP built-in object events are events used in ASP to perform actions at the beginning or end of an application or session. They can be:
Scope = either Session or Application
Identifier = the name for the object instance
progID = an identifier associated with a class identifier
ClassID = a unique identifier for a COM class object
Either progID or ClassID must be specified
Finally the TypeLibrary declaration which has the following syntax:
file = absolute path to a type library
typelibraryuuid = universally unique identifier for the type library
majorversionnumber and minorversionnumber = used for selecting version (optional)
localeid = the locale identifier to be used for the type library
We can write any type of script in the global.asa file. It doesn't matter the language, as long as it is supported. However we must declare the language inside the As you can see we use the Session_OnStart and Session_OnEnd to handle events. Then we just need to put on any .asp page the following code, where we actually need to show the current user count:
The script is working quite well enough. The only thing we should remember is that we rely on sessions and that the session timeout plays an important role. In fact, the script increases the user count when the session starts (and that's ok), and decreases the user count when the session ends. If the session has a long timeout, the user will be counted as online while it might be already somewhere else and the session still alive.
That's all. Please let me know your thoughts in the comment section below.
The global.asa
The global.asa is a small and optional file that you create in the root of your web site. We use the file to specify event scripts and objects that have session or application scope. As I said in the previous article (where we talked about sessions), the global.asa can be used to handle sessions - or, more specifically, to bind events to session. There must be only one global.asa in the root of your web site and it can contain only:
- ASP built-in object events;
- object declarations;
- TypeLibrary declarations.
The ASP built-in object events are events used in ASP to perform actions at the beginning or end of an application or session. They can be:
- Application_OnStart
- Application_OnEnd
- Session_OnStart
- Session_OnEnd
where: Scope = either Session or Application
Identifier = the name for the object instance
progID = an identifier associated with a class identifier
ClassID = a unique identifier for a COM class object
Either progID or ClassID must be specified
Finally the TypeLibrary declaration which has the following syntax:
where:file = absolute path to a type library
typelibraryuuid = universally unique identifier for the type library
majorversionnumber and minorversionnumber = used for selecting version (optional)
localeid = the locale identifier to be used for the type library
We can write any type of script in the global.asa file. It doesn't matter the language, as long as it is supported. However we must declare the language inside the As you can see we use the Session_OnStart and Session_OnEnd to handle events. Then we just need to put on any .asp page the following code, where we actually need to show the current user count:
<%= Application("WhoOn") %> USERS CURRENTLY ONLINE
The original idea of the script was conceived by the powerasp web site, so please refer to the link for more information.The script is working quite well enough. The only thing we should remember is that we rely on sessions and that the session timeout plays an important role. In fact, the script increases the user count when the session starts (and that's ok), and decreases the user count when the session ends. If the session has a long timeout, the user will be counted as online while it might be already somewhere else and the session still alive.
That's all. Please let me know your thoughts in the comment section below.