-Log in
-Register (free)


-RSS Feeds
-New MyDesk Module
-Update to Profile

I know this is an ASP forum but...
Charting with ASP
Subscript out of range




 Lesson 19: Global.asa
Lesson 19: Global.asa

Author: Alexander Haneng
Difficulty: Medium
Requires: ASP
Demo: n/a
Download: n/a
Summary:
A quick guide to the mysterious file called Global.asa.

Intro:
I got a question by mail if I could write a lesson about Global.asa. Global.asa is the "dark file of ASP" and many don't even know that it exits. The truth is that Global.asa can be a powerful tool, but it's not as good at it could and should be, and is often difficult to master. I thought Global.asa could be good material for a lesson or two so let me try to shed some light on this "dark file". The following lesson is based upon my own experiences and I may be mistaken in some areas.

What is Global.asa?
This is most likely your first question. Well I'm not sure of the right term for it, but I can explain its function (or more correct: how it should work). Ok here we go: Global.asa is a file that can contain four scripts. One script that is run when IIS/PWS is started (Application_OnStart) and one script when IIS/PWS is ended (Application_OnEnd). (Usually these are run when your machine is rebooted). In addition you have two scripts that are run when a user starts his/hers session (Session_OnStart) (his/hers visit) and when it's ended (Session_OnEnd)(when the session runs out).

The bad news
This sounds like a terrific file, but there are some flaws. The first problem is that the Application_OnEnd script doesn't run. At least nobody that I have talked two have managed to get it to work. This script would be so great, since that means that you can have a script that saves all your global variables into a file when IIS/PWS is ended, and then you can load this info back in when IIS/PWS is started again (with Application_OnStart).

The next "bad news" is that sessions only work with visitors that have cookies enabled. So if you have a script that uses the Session_OnStart script to add 1 to your visitor counter, it will count a user with cookies turned off several times as he view pages on your website.

So what else sucks with Global.asa? Well one thing that irritates me is that it's hard to refresh when you make a change to the script. Usually you have to reboot the machine to get it refreshed. This problem seams to occur a bit randomly, I just sat up a NT 4.0 IIS 4.0 machine with a new global.asa file and the server would only serve the message "Application Restarting" to the visitors no matter how long I waited. A quick reboot fixed it. But the next time I modified the Global.asa file it worked without a problem or restart.

The good news
Well Global.asa is a terrific tool when you want to run some script at startup, or when users visit and exits your site. I use it to load variables, count visitors/page views and a whole lot more. Another excellent use is to let Global.asa take care of tasks that have to be run on regular times: cleaning up global variables, databases etc. Just make a global variable for a counter, for each session you add one and when you hit e.g. 10 it runs a script (including the task) and resets the counter to 0.

The "magic" code
So how do I make a global.asa file? This is a question I get a lot. Well to save you some time I have made it for you. Here it is:

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
'Add your Application_OnStart code here
End Sub
</SCRIPT>

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnEnd
'Add your Application_OnEnd code here
End Sub
</SCRIPT>


<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Session_OnStart
'Add your Session_OnStart code here
End Sub
</SCRIPT>


<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Session_OnEnd
'Add your Session_OnEnd code here
End Sub
</SCRIPT>


So how do I use this code? Just put it into a plain textfile, add your scripts, and save it as Global.asa (not Global.asp or Global.asa.asp) in the root directory of your webserver. Can you have multiple Global.asas? Yes you can, but only one in each directory. An ASP script will search for its "closest" Global.asa file and use that. The Global.asa file cannot contain any HTML content. So you can't have a JavaScript popup window script in the Session_OnStart script. And a warning to all you MS FrontPage users: FRONTPAGE WILL MESS WITH YOUR GLOBAL.ASA FILE AND TRY TO ADD HTML TAGS INTO IT MAKING YOUR ASP SITE CRASH!! (Sorry about the capital letters, but I won't take the risk of getting a zillion mails with: "I followed your Global.asa lesson, but I can't get it to work, and no my site doesn't work either. I use FrontPage. Please help.") So please use Notepad for this, OK?

Some info
Just have to include a little something about the Session_OnEnd. How does IIS/PWS know when a user leaves your site? It doesn't! It just have a build in time limit (defaulted to 20 minutes), so if it hasn't got a new request from that users whit in that time limt it guesses that the user have left.

The end
Well, I believe I have written the most important things about Global.asa. Maybe I'll write another article about this file going a bit deeper into things. Time will tell. Until then I hope you will at least try to use Global.asa. Good luck!

Where to go next:
Check out the other lessons.


18: Turning numbers into graphs
20: DSN-less MS Access connection
| Info |
© Copyright 1997-2009 Alexander Haneng