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.
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
<OBJECT RUNAT=Server SCOPE=Scope ID=Identifier { PROGID="progID"|CLASSID="ClassID"} >
...
</OBJECT>
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:
<!-- METADATA TYPE="TypeLib"
FILE="file"
UUID="typelibraryuuid"
VERSION="majorversionnumber.minorversionnumber"
LCID="localeid"
-->
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 <script> tag.
How to show current users on a web page
Said all that, we can use the session event handlers to easily show the current users on a web page. It must be said that the following procedure is not perfect but gives a fairly good count of online users. To do so, we open or create a global.asa file in the root of our web site and put the following code inside:
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
Application("WhoOn") = 0
End Sub
Sub Session_OnStart
Application.Lock
Application("WhoOn") = Application("WhoOn") + 1
Application.Unlock
End Sub
Sub Session_OnEnd
Application.Lock
Application("WhoOn") = Application("WhoOn") - 1
Application.Unlock
End Sub
</SCRIPT>
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.
0 thoughts:
Post a Comment
Comments are moderated. I apologize if I don't publish comments immediately.
However, I do answer to all the comments.