|
A session starts when an user
browses a site and stays open until he leaves the site (with timeout
period) or closes his browser. An application starts when the
particular iis site is started. On iis a site can be an iis
application or a virtual directory. Global.asa file (stays on the
root directory) is important (although optional) as it can hold
subroutines that executes when an application or session is started
or stopped. These functions can perform routine tasks when the user
visits the site or when a site is started.
Session , application and
global.asa file
In the first example we are setting
values to session and application variables. Please note that
session and application variables are different.
<%
'Set session variable. These values will be available as
long as the browser or site is open.
Session("name")= "jerry"
Session("age")= 4
Session("count")= 1
'Add a number to session value
Session("count")= Session("count")+1
%>
<hr>
Session name is:
<%=Session("name")%><br>
Session age is<%=Session("age")%><br>
Value of counter is now:
<%=Session("count")%><br>
<hr>
<%
' Similarly the values can be stored in an application
variable, which will beavailable as long as the website is
running.
Application("name")= "jerry"
Application("age")= 4
Application("count")= 1
'Add a number to application value
application("count")= application("count")+1
%>
Application name is:
<%=Application("name")%><br>
Application age is<%=Application("age")%><br>
Value of Application counter is now:
<%=Application("count")%><br> |
Here are the functions
inside a global.asa file. We are just initializing some variables.
You can also write other functions an subroutines and call them from
these functions.
<SCRIPT
LANGUAGE=VBScript RUNAT=Server>
Sub Application_OnStart
Application("name")= "jerry"
Application("age")= 4
Application("count")= 1
End Sub
Sub Application_OnEnd
Application("name")= ""
Application("age")= 0
Application("count")= 0
End Sub
Sub Session_OnStart
Session("name")= "jerry"
Session("age")= 4
Session("count")= 1
End Sub
Sub Session_OnEnd
Session("name")= ""
Session("age")= 0
Session("count")= 0
End Sub
</script> |
[Download
Sample Code]
|