Introduction To The GLOBAL.ASA For ASP Programmers - Inside the GLOBAL.ASA File
(Page 2 of 4 )
Lets get a little more detailed about what can be contained inside the GLOBAL.ASA. For starters, this file can contain certain session and application events. These events tell the internal application and session objects what to do when they start and end. Below are the four possible events that may be included in this file:
• Application_OnStart – This event is called the FIRST time any user hits an ASP page in the same tree as the GLOBAL.ASA. The first hit of any user is considered to be when the application is first invoked. This function is good for loading any default values for application variables, and initializing logs files and such. The application state is reset after the Web Server is restarted, or when the GLOBAL.ASA file is modified. The Session_OnStart will automatically fire when this event finishes.
• Session_OnStart – This event is called the first time each new user hits the ASP application. So, for each session that is created, this event is called. This event is good for setting up any session level defaults or variables that will be needed for the duration of the user session.
• Session_OnEnd – This even will fire when a user ends a session. This can be done by a user logging out manually, or via a specified timeout period (by default 20 minutes). Useful for session cleanup code.
• Application_OnEnd – This final event occurs when the last user ends his or her session. This process normally fires when the web server is being shut down. This is useful for closing files, creating reports, and updating certain usage statistics.
Here is an example of using the GLOBAL.ASA to create a simple hit counter:
<SCRIPT language=vbscript runat="server">
Sub Application_OnStart
getcounter
End Sub
Sub Session_OnStart
Application.Lock
Application("currentCount")=Application("CurrentCount")+1
Application.UnLock
End Sub
Sub Application_OnEnd
Writecounter
End Sub
sub getcounter
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/counter.mdb"
set rs=conn.execute("select currentcount from datatable”)
Application("currentCount")=rs(“currentcount”)
rs.Close
conn.Close
end sub
sub writecounter
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/counter.mdb"
conn.execute("update datatable set currentcount = “ &
application(“currentcount”))
conn.Close
end sub
</SCRIPT>
In order to use the counter value on any webpage, you would do the following:
This is my web page There have been
<%response.write(Application("currCount"))%>Visitors to this website!
You will notice in the GLOBAL.ASA example above that I actually use subroutines that are called from the main events. I did this for code readability, and it is a common practice. You will also notice that there are no <% and %> tags inside the GLOBAL.ASA file. In this file, you must place all script within the <script> tag.
Next: Application and Session Objects >>
More ASP Articles
More By Rich Smith