Maintaining Session State With ASP - The Application Object
(Page 3 of 5 )
The Application object is used to store variables and to access variables from any page. All users share one Application object.
All variables created within the application object have application level scope, meaning that they are accessible to all users who visit your site. All ASP pages in a virtual directory and its subdirectories come under the application scope, so more than one user shares application level variables at any time.
Syntax: Application.method
Methods: - Lock: Prevents other users from changing the application objects properties
- Unlock: Allows other users to change the application objects properties
Events: - Application_OnEnd: This routine is called when all user sessions expire and the application quits. This event lives in the global.asa file, which we will look at shortly.
- Application_OnStart: This routine is called when the application object is first references. This event lives in the global.asa file as well.
Example:
This example uses an application variable to determine the uptime of the server. To achieve this we simply add an application variable to the OnStart event in the global.asa file, which needs to exist in the root directory of your web server:
<script language="VBScript" runat="Server">
Sub Application_OnStart
Application("startTime") = Now
End Sub
</script>
The following VBScript will display the uptime:
<% @ language="vbscript" %>
<% Option Explicit %>
<%
Dim days, hours, minutes, seconds, startTime, runTime
' Read the start time from the
' Application variable
startTime = CDate(Application("startTime"))
' Take the difference
runTime = CDate(Now - startTime)
' Calculate the time components
If DateDiff("d", startTime, Now) = 0 Then
days = 0
Else
days = Day(runTime)
End If
hours = Hour(runTime)
minutes = Minute(runTime)
seconds = Second(runTime)
' Display the uptime
Response.Write "Uptime: " & days & " days(s), "
Response.Write hours & " hour(s), "
Response.Write minutes & " minute(s), and "
Response.Write seconds & " second(s)"
%>
Next: The Global.asa File >>
More ASP Articles
More By Himanshu Khatri