Chapter 39: ASP Global.asa file
1. What is the global.asa file?
global.asa is a special file that must be placed in the root folder of your Classic ASP website.
It is not a normal .asp page — it never gets served directly to the browser (if someone tries to access http://yoursite.com/global.asa → 404 or forbidden).
Instead, IIS automatically runs code inside global.asa at four very specific moments in the life of the application:
| Event name | When it runs | Typical use case in real Classic ASP sites |
|---|---|---|
| Application_OnStart | First time any user visits the site after IIS restart or app pool recycle | Set global variables, cache lookup tables, open shared connections, initialize counters |
| Application_OnEnd | When the application shuts down (IIS restart, app pool recycle, server shutdown) | Clean up resources, log shutdown time (rarely used) |
| Session_OnStart | Every time a new user session begins | Initialize per-user variables, log new visitor, set default language |
| Session_OnEnd | When a user session ends (browser closes or Session.Abandon or timeout) | Log logout time, clean up per-user resources (rarely reliable) |
Key facts you must remember:
- File name must be exactly global.asa (lowercase, .asa extension)
- Must be in the root folder of the website (not in a subfolder)
- Uses special <script runat=”server”> tags — not normal <% %> blocks
- Only VBScript or JScript allowed (VBScript is 99% of cases)
2. Basic Structure of global.asa
|
0 1 2 3 4 5 6 |
<script language="VBScript" runat="server"> Sub Application_OnStart ' Runs once when the first user hits the site Application("SiteName") = "Webliance Hyderabad Store" Application("SupportEmail") = "support@webliance.in" Application("VisitorCount") = 0 Application("LastRestart") = Now() ' Optional: cache lookup data ' Call LoadCategoriesIntoApplication() End Sub Sub Application_OnEnd ' Runs when application shuts down (rarely useful) ' Could log to file: "Application stopped at " & Now() End Sub Sub Session_OnStart ' Runs for every new visitor Application.Lock Application("VisitorCount") = Application("VisitorCount") + 1 Application.Unlock ' Optional: set default user preferences Session("Language") = "English" Session.Timeout = 30 End Sub Sub Session_OnEnd ' Runs when session ends — NOT reliable for logout tracking ' Browser close, timeout, or Session.Abandon triggers it End Sub </script> |
Very important notes:
- All four Subs are optional — you can have only the ones you need
- Application.Lock / Unlock is mandatory when modifying Application variables
- Session_OnEnd is not reliable for critical cleanup — many browsers don’t fire it when tab closes
3. Real-World Example 1 – Visitor Counter + Site Settings
Most classic ASP homepages in 2000–2005 had a “You are visitor #XXXXX” line — this is how it was done.
|
0 1 2 3 4 5 6 |
<!-- In global.asa --> <script language="VBScript" runat="server"> Sub Application_OnStart Application("SiteName") = "My Hyderabad Online Shop" Application("VisitorCount") = 0 End Sub Sub Session_OnStart Application.Lock Application("VisitorCount") = CLng(Application("VisitorCount")) + 1 Application.Unlock End Sub </script> |
Then in any page (e.g. default.asp):
|
0 1 2 3 4 5 6 |
<p> Welcome to <%= Application("SiteName") %>!<br> You are visitor number <%= Application("VisitorCount") %>. </p> |
4. Real-World Example 2 – Cached Categories Menu (Very Common)
|
0 1 2 3 4 5 6 |
<!-- global.asa --> <script language="VBScript" runat="server"> Sub Application_OnStart Dim conn, rs, dict Set dict = Server.CreateObject("Scripting.Dictionary") Set conn = Server.CreateObject("ADODB.Connection") conn.Open "Provider=SQLNCLI11;Server=.\SQLEXPRESS;Database=MyShop;Trusted_Connection=yes;" Set rs = conn.Execute("SELECT CategoryID, CategoryName FROM Categories ORDER BY CategoryName") Do While Not rs.EOF dict.Add rs("CategoryID"), rs("CategoryName") rs.MoveNext Loop Application("Categories") = dict rs.Close conn.Close End Sub </script> |
Then in any page (e.g. sidebar include):
|
0 1 2 3 4 5 6 |
<h3>Categories</h3> <ul> <% Dim catID, catName For Each catID In Application("Categories") catName = Application("Categories")(catID) Response.Write "<li><a href='products.asp?cat=" & catID & "'>" & Server.HTMLEncode(catName) & "</a></li>" Next %> </ul> |
→ Database hit once on app start → every page reads from memory → fast!
5. Important Rules & Warnings (2026 Reality)
- global.asa must be in root folder — not in /includes or subfolder
- Only one global.asa per site — IIS ignores others
- Changes to global.asa require IIS restart or app pool recycle to take effect
- Lock / Unlock every time you write to Application variables
- Do NOT store sensitive data in Application — it is readable by all users
- Session_OnEnd is unreliable — many browsers don’t trigger it on tab close
- In 2026 legacy systems → still used for config, counters, cached menus, but very risky if not locked properly
6. Teacher Summary – The global.asa File in Classic ASP
global.asa is:
- A special file in the root folder that never gets served to browsers
- Contains four special Subs: Application_OnStart, Application_OnEnd, Session_OnStart, Session_OnEnd
- Runs automatically on app start / new session / shutdown
- Main uses: set global constants, cache lookup data, initialize counters, set session defaults
- Application_OnStart = perfect place to load shared config or cache
- Session_OnStart = good for visitor counters, default preferences
- Lock / Unlock mandatory when modifying Application variables
- Session_OnEnd — avoid relying on it for cleanup
This is how millions of Classic ASP sites initialized global settings, cached data, and tracked visitors — and many legacy Indian internal systems, ERPs, and small-business portals still use exactly this global.asa pattern in 2026.
Next class?
- Want a full example with global.asa + Session + Cookies + login?
- Or how to debug when global.asa doesn’t run?
- Or compare global.asa vs modern ASP.NET Core Startup/Program.cs?
- Or move to the next W3Schools topic (ASP Include again or ASP Cookies)?
Just tell me — I’m here! 🚀🇮🇳 Keep going strong, Webliance! 😊
