ASP Application Variables and Global.asa

Application variables store data shared by every visitor to an ASP application at once, and Global.asa defines special event procedures such as Application_OnStart and Session_OnStart that ASP runs automatically at the right moments.

Application Variables Are Shared By Everyone

Unlike Session variables, which are private to one visitor, Application variables belong to the whole ASP application and are visible to every visitor at the same time. They suit data that should be identical for all users, such as a site-wide counter, a cached list of categories, or a setting read once from a configuration file.

Reading and Writing an Application Variable

<%
  Application("siteName") = "My ASP Store"
  Response.Write "Welcome to " & Application("siteName")
%>

Protecting Application Variables With Lock and Unlock

Because many visitors can hit the server at the same instant, two requests could try to update the same Application variable at once and corrupt the value. Application.Lock and Application.Unlock ensure only one script at a time can change a shared variable, which matters most when incrementing a counter.

A Site-Wide Visitor Counter

<%
  Application.Lock
  Application("totalVisitors") = Application("totalVisitors") + 1
  Application.Unlock

  Response.Write "Total visitors so far: " & Application("totalVisitors")
%>

Global.asa: Setting Up Values Once

Global.asa is a special file in the root folder of an ASP application. Browsers never request it directly; instead, IIS reads it to find event procedures that run automatically at specific moments, such as Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd.

Global.asa

<script language="VBScript" runat="server">

Sub Application_OnStart
  ' Runs once, the very first time anyone requests a page in this app
  Application("totalVisitors") = 0
  Application("siteName") = "My ASP Store"
End Sub

Sub Application_OnEnd
  ' Runs once, when the application shuts down (e.g. IIS restarts)
End Sub

Sub Session_OnStart
  ' Runs every time a new visitor's session begins
  Session("cartTotal") = 0
  Session.Timeout = 20
End Sub

Sub Session_OnEnd
  ' Runs when a visitor's session times out or is abandoned
End Sub

</script>
  • Application_OnStart fires exactly once, before the first page of the application is ever served.
  • Session_OnStart fires once per visitor, the first time that visitor requests any page.
  • Session_OnEnd fires once per visitor, when their session times out or Session.Abandon is called.
  • Application_OnEnd fires once, when the whole application stops (for example, when IIS restarts).
Session VariablesApplication Variables
ScopeOne visitor onlyEvery visitor, shared
Typical useLogin name, cart contentsVisitor counter, cached settings
Needs locking?No, it's private to one visitorYes, use Lock/Unlock when writing
Note: Keep the code inside Application_OnStart and Session_OnStart fast and simple. Every visitor waits on Session_OnStart before their very first page can load, so slow database calls or file operations there make your whole site feel sluggish.

Exercise: ASP Application

How does an Application variable differ from a Session variable?