| Topic:
ASP
The Global.asa
The Global file is an optional file in which you can specify event
scripts and declare objects that have session or application scope.
It is not a content file displayed to the users; instead it stores
event information and objects used globally by the application. This
file must be named Global.asa and must be stored in the root directory
of the application. An application can only have one Global.asa file.
Application - An application (in ASP terms) is basically your
asp site. Usually this is a virtual directory or maybe the whole thing. An
application starts when the web server starts and ends when the web server
stops. Which means that whatever variable you put in to start when the application
starts (we'll cover that soon) won't stop till it stops.
Session - A session is from the time that a user starts looking
at your site to the time he/she stops. Session is kept track of by a cookie
on the user that gets deleted when the user closes his/her browser. All session
data is stored on the server.
Global.asa files can contain only the following:
- Application events
- Session events
- <OBJECT> Declarations
- TypeLibrary Declarations
Application_OnStart()
The minute the first user fetches the first file in your Web Application
directory an application_onstart event occurs. It will never occur again
until the web server stops and then restarts.
Session_OnStart()
The code in this event is fired for each user before their first page
is fetched and displayed the instant that they access any resources
or pages inside your Web Application.
Session_OnEnd
Session_OnEnd fires when the session ends and not when the user leaves
your site or closes their browser. So by default this happens 20 minutes
later and the user is long gone! This is often a source of much confusion
for beginners, if you want to use this to log when the last request
happened on your site you can use the DateAdd() function to get the
time of the last request.
Application_OnEnd()
This event is fired if the Web Application stops.
Creation of Global.asa
<script language="VBScript" RUNAT="SERVER">
Sub Application_OnStart()
Set objConn = CreateObject("ADODB.Connection")
Set Application("objConn") = objConn
Set Application("stTime") = CStr(Now)
End Sub
Sub Application_OnEnd()
Set Application("objConn") = Nothing
Set objConn = Nothing
End Sub
Sub Session_OnStart()
Application.Lock
Application("visitors") = Application("visitors")
+ 1
Session("visitor") = Application("visitors")
Application.Unlock
End Sub
Sub Session_OnEnd()
End Sub
</script>
When the Application Starts - An ADODB.Connection
object is created and made an application variable of the same name.
The current time is stored as a string as well, in an application
variable.
- When the Application Ends - The connection object
gets set to nothing.
- When a Session Starts - We increment the visitor
counter by 1 and set that value to the application variable and the
session variable. We use Application.Lock and Unlock to make sure
that no-one else updates the value at the same time (things could
get messy).
- When the Session Ends - Nothing happens.
|