Global.asa is a text file locate in your main directory (/global.asa). The
Global.asa file is an optional file that can contain declarations of objects,
variables, and methods that can be accessed by every page in an ASP application.
A quick guide to the mysterious file called Global.asa.
The Global.ASA File
The Global.asa file is an optional file that can contain declarations
of objects, variables, and methods that can be accessed by every page
in an ASP application. All valid browser scripts (JavaScript, VBScript,
JScript, PerlScript, etc.) can be used within Global.asa.
The Global.asa file can contain only the following :-
- Application events
- Session events
- <Object> declarations
- Type Library declarations
- The # include directive
Note :- The Global.asa file must be stored in the root directory
of the ASP application, and each application can only have one Global.asa
file.
Events in Global.ASA
In Global.asa you can tell the application and session objects what to
do when the application/session starts and what to do when the application/session
ends. The code for this is placed in event handlers. The Global.asa file
can contain four types of events:
- Application_OnStart - This event occurs when the FIRST user calls
the first page from an ASP application. This event occurs after the
Web server is restarted or after the Global.asa file is edited. The
"Session_OnStart" event occurs immediately after this event.
- Session_OnStart - This event occurs EVERY time a NEW user requests
his or her first page in the ASP application.
- Session_OnEnd - This event occurs EVERY time a user ends a session.
A user ends a session after a page has not been requested by the user
for a specified time (by default this is 20 minutes).
- Application_OnEnd - This event occurs after the LAST user has ended
the session. Typically, this event occurs when a Web server stops. This
procedure is used to clean up settings after the Application stops,
like delete records or write information to text files.
like delete records or write information to text files.
Note :- The Global.asa file must be stored in the root directory
of the ASP application, and each application can only have one Global.asa
file.
The Syntax is as follows :-
<object runat="server"
scope="scope" id="id"{progid="progID"|classid="classID"}>....</object> |
A Global.asa file could Look something Like This
:-
<script language="vbscript"
runat="server">
sub Application_OnStart
'some code
end sub
sub Application_OnEnd '
some code
end sub
sub Session_OnStart
'some code
end sub
sub Session_OnEnd '
some code
end sub
</script> |
Note :- Because we cannot use the ASP script delimiters (<%
and %>) to insert scripts in the Global.asa file, we put subroutines
inside an HTML <script> element.
Example#1 :-
Active Users Counter
Just copy the code in the table to a text file and save it in the main
directory of your site ("/global.asa").
global.asa
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Sub Application_OnStart application("activevisitors")=0
End Sub
Sub Application_OnEnd
End Sub
Sub Session_OnStart application.lock application("activevisitors")=application("activevisitors")+1
application.unlock End Sub Sub Session_On
End application.lock application("activevisitors")=application("activevisitors")-1
application.unlock
End Sub
</SCRIPT>
12345678910111213141516171819202122 |
In case we want to show the number of visitors in our page, we must use
this kind of code :-
index.asp
There are <% =application("activevisitors") %> active
visitors. |
Example#2 :--
In this example we will create a Global.asa file that counts the number
of current visitors.
The Global.asa file :-
<script language="vbscript" runat="server">
Sub Application_OnStart
Application("visitors")=0
End Sub
Sub Session_OnStart
Application.Lock
Application("visitors")=Application("visitors")+1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors")=Application("visitors")-1
Application.UnLock
End
Sub</script>
|
To display the number of current visitors in an ASP file :-
<html>
<head>
</head>
<body>
<p>
There are <%response.write(Application("visitors"))%>
online now!
</p>
</body>
</html> |
|