Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems using Application variables...

Status
Not open for further replies.

juststarted

IS-IT--Management
Jul 6, 2004
4
GB
Hi ASP People,
I just started using ASP and already have encountered a problem trying to get this simple code to work. I want to count a number of current users of the application and it looks like it just keeps to grow. I close browser window - the count doesn't change. I open new browser window - the count increments by one.

here's the code:


global.asa

Sub Application_OnStart
Application("usCount") = 0
End Sub
Sub Session_OnStart
Application.Lock
Application("usCount")=Application("usCount")+1
Application.UnLock
End Sub
Sub Session_OnEnd
Application.Lock
Application("usCount")=Application("usCount")-1
Application.UnLock
End Sub


default.asp


<%@ Language=VBScript %>
<%Response.Buffer=0%>
<HTML>
<HEAD>
<META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;>
<meta http-equiv=&quot;refresh&quot; content=&quot;3&quot;>
</HEAD>
<BODY>

<h1>Welcome!!!</h1><br><br><br><br>
<%Application.Lock%>
You are the user number <%=Application(&quot;usCount&quot;)%>
<%Application.UnLock%>

</BODY>
</HTML>


Both pages are in the application.

So, could anyone find out what I'm doing wrong?
Thanks a lot.
 
Hi,

you have mixed up application and session level counts. Application counts start when the server starts up and session variables start when someone access the page via the browser. So your really need two variables, one for application which will show you the number of users who have accessed the site in total. The other, a session level variable, will count the number of users who are currently on the site....


Sub Application_OnStart
Application(&quot;usCount&quot;) = 0
Application.Lock
Application(&quot;usCount&quot;)=Application(&quot;usCount&quot;)+1
Application.UnLock
End Sub

Sub Session_OnEnd
Application.Lock
Application(&quot;SessCount&quot;)=Application(&quot;SessCount&quot;)+1
Application.UnLock
End Sub


Sub Session_OnEnd
Application.Lock
Application(&quot;SessCount&quot;)=Application(&quot;SessCount&quot;)-1
Application.UnLock
End Sub

hth
Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
sorry this should be

Sub Session_OnStart
Application.Lock
Application(&quot;SessCount&quot;)=Application(&quot;SessCount&quot;)+1
Application.UnLock
End Sub
Bastien

There are many ways to skin this cat,
but it still tastes like chicken
 
Thanks, Bastien

I got the idea. Will try.
Good luck to you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top