๐ Classic ASP โ Global.asa
โ Configure Application and Session Events
๐งฒ Introduction โ What Is Global.asa
in Classic ASP?
In Classic ASP, the Global.asa
file is a special configuration file that defines global settings and event-handling logic for your ASP application. It enables you to manage key lifecycle events such as when an application or session starts or ends, and to register objects and variables at a global level.
๐ฏ In this guide, youโll learn:
- What the
Global.asa
file is and where to place it - The four key ASP event handlers inside
Global.asa
- How to use Application and Session objects for shared data
- Real-world examples and best practices
๐ Where Is Global.asa
Located?
- It must be placed in the root directory of your ASP application.
- It is automatically recognized by IISโyou donโt need to include or link it.
/MyASPApp/
โ
โโโ global.asa
โโโ index.asp
โโโ login.asp
๐งฑ Structure of Global.asa
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart
' Initialize application-wide variables
Application("AppName") = "My Classic ASP Site"
End Sub
Sub Application_OnEnd
' Clean up application resources
End Sub
Sub Session_OnStart
' Initialize session variables
Session("UserName") = "Guest"
End Sub
Sub Session_OnEnd
' Clean up session data
End Sub
</SCRIPT>
๐งฉ Event Handlers in Global.asa
Event | Description |
---|---|
Application_OnStart | Runs once when the application first starts (after restart or deployment) |
Application_OnEnd | Runs once when the application shuts down (e.g., server stop) |
Session_OnStart | Runs each time a new user session begins |
Session_OnEnd | Runs when a session times out or is explicitly ended |
๐ฅ Example โ Set App and Session Variables
Sub Application_OnStart
Application("Version") = "1.0"
Application("TotalVisits") = 0
End Sub
Sub Session_OnStart
Application("TotalVisits") = Application("TotalVisits") + 1
Session("IsLoggedIn") = False
End Sub
๐งช Output when Session_OnStart
fires:
TotalVisits
increasesIsLoggedIn
is initialized
๐ Accessing Values Set in Global.asa
<%
Response.Write "App Version: " & Application("Version") & "<br>"
Response.Write "Visits: " & Application("TotalVisits") & "<br>"
Response.Write "Logged In: " & Session("IsLoggedIn")
%>
โ๏ธ Session Timeout Configuration (Optional)
To set the default session timeout globally:
Sub Session_OnStart
Session.Timeout = 20 ' In minutes
End Sub
โ Limitations of Global.asa
Limitation | Notes |
---|---|
Works only in IIS | Not recognized in non-Microsoft environments |
Cannot use external includes | #include does not work inside Global.asa |
No HTML allowed | It is not a webpage, only ASP/VBScript logic in <SCRIPT> tags |
๐ Best Practices for Global.asa
โ Do:
- Keep
Global.asa
lean and event-driven - Use it for app-wide counters, configs, and session tracking
- Store read-only settings or pre-load data
โ Avoid:
- Storing sensitive information
- Running complex logic or DB operations inside event handlers
- Using it to replace modular includes or config systems
๐ Summary โ Recap & Next Steps
The Global.asa
file is a central event script that lets you manage application and session lifecycle events. Itโs a powerful feature in Classic ASP for controlling global behavior and initializing default states.
๐ Key Takeaways:
- Handles
Application_OnStart
,Session_OnStart
, and their end events - Ideal for initializing counters, configs, and default values
- Placed in the application root; no need to reference manually
โ๏ธ Real-world Use Cases:
- Visitor counters
- Site-wide configuration variables
- Default session state like “guest” access
โ FAQs โ Classic ASP Global.asa
โ Where should I place the Global.asa
file?
โ
It must be in the root directory of your Classic ASP application.
โ Do I need to include or reference Global.asa?
โ
No. IIS automatically detects and runs it when appropriate.
โ Can I use #include
inside Global.asa?
โ No. #include
directives are not supported in Global.asa
.
Share Now :