By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,288 Members | 3,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,288 IT Pros & Developers. It's quick & easy.

Count of current visitors

P: n/a
cc


From the popular website At this link:
http://www.w3schools.com/asp/asp_globalasa.asp

I got this code, but it does not work on my server... I can open three
IE browsers and watch the count go to three. Upon closing two, each
having separate sessions (and session ID's of course), the count
remains at three! I'm not looking for alternative ways of doing this:
I'm interested in knowing why the application variable does not get
reduced upon ending the session by closing the browser. Any ideas?

Thanks,
See below:

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>

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Did you refresh your 1 page after closing 2 and 3 ?
Jul 19 '05 #2

P: n/a
cc@cc.com wrote:
From the popular website At this link:
http://www.w3schools.com/asp/asp_globalasa.asp

I got this code, but it does not work on my server... I can open three
IE browsers and watch the count go to three. Upon closing two, each
having separate sessions (and session ID's of course), the count
remains at three! I'm not looking for alternative ways of doing this:
I'm interested in knowing why the application variable does not get
reduced upon ending the session by closing the browser. Any ideas?

This seems to be a very prevalent misconception.

The session does not end when the user closes his browser. Think about it.
How can the server know when a user closes his browser? Answer: it can't.
HTTP is a stateless protocol. There is no "signal" sent to the server when a
user closes his browser, or navigates to another website. The only way a web
server has of knowing a session is still in use is if it receives requests
for pages with the session ID in the request headers.

There are only two ways a session will end:

1. A Session.Abandon statement is executed
2. The session's timeout period is exceeded, i.e., the server receives no
new requests for pages containing that session id for a longer period of
time than the session's timeout setting.

So, in the scenario you describe above: if your session timeout setting is
at the default value of 20 min., the user count will get adjusted 20 min.
after the user closes his browser.

Aaron describes a way to get a more accurate count of current users using a
database in this article:
http://www.aspfaq.com/show.asp?id=2491

HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Jul 19 '05 #3

P: n/a
Sessions timeout after n minutes (default is 20 I think) so you'll
have to be patient (or change the setting)

cc@cc.com wrote in message news:<41***************@news.dallas.sbcglobal.net> ...
From the popular website At this link:
http://www.w3schools.com/asp/asp_globalasa.asp

I got this code, but it does not work on my server... I can open three
IE browsers and watch the count go to three. Upon closing two, each
having separate sessions (and session ID's of course), the count
remains at three! I'm not looking for alternative ways of doing this:
I'm interested in knowing why the application variable does not get
reduced upon ending the session by closing the browser. Any ideas?

Thanks,
See below:

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>

Jul 19 '05 #4

P: n/a
> > I got this code, but it does not work on my server... I can open three
IE browsers and watch the count go to three. Upon closing two, each
having separate sessions (and session ID's of course), the count
remains at three! I'm not looking for alternative ways of doing this:
I'm interested in knowing why the application variable does not get
reduced upon ending the session by closing the browser. Any ideas?

This seems to be a very prevalent misconception.

The session does not end when the user closes his browser. Think about it.
How can the server know when a user closes his browser? Answer: it can't.

True, and I think people get confused because if they do close a
browser window, then open a new one, they get a new session.
Jul 19 '05 #5

P: n/a
cc
On Mon, 2 Aug 2004 06:29:21 -0400, "Bob Barrows [MVP]"
<re******@NOyahoo.SPAMcom> wrote:

I've been developing on the IIS platform since '98, and I've never
needed this counter functionality before. And this caught me off
guard. OF COURSE! The browser isn't going to send a last http call
before the .exe ends, so the server won't know when it closes.

I could SWEAR I read in one of those WROX books that session ended on
closing the browser, and that kinda' stuck with me.

Anyway... what's a 20 minute delay in the scope of all the other stuff
we have to deal with.
cc@cc.com wrote:
From the popular website At this link:
http://www.w3schools.com/asp/asp_globalasa.asp

I got this code, but it does not work on my server... I can open three
IE browsers and watch the count go to three. Upon closing two, each
having separate sessions (and session ID's of course), the count
remains at three! I'm not looking for alternative ways of doing this:
I'm interested in knowing why the application variable does not get
reduced upon ending the session by closing the browser. Any ideas?

This seems to be a very prevalent misconception.

The session does not end when the user closes his browser. Think about it.
How can the server know when a user closes his browser? Answer: it can't.
HTTP is a stateless protocol. There is no "signal" sent to the server when a
user closes his browser, or navigates to another website. The only way a web
server has of knowing a session is still in use is if it receives requests
for pages with the session ID in the request headers.

There are only two ways a session will end:

1. A Session.Abandon statement is executed
2. The session's timeout period is exceeded, i.e., the server receives no
new requests for pages containing that session id for a longer period of
time than the session's timeout setting.

So, in the scenario you describe above: if your session timeout setting is
at the default value of 20 min., the user count will get adjusted 20 min.
after the user closes his browser.

Aaron describes a way to get a more accurate count of current users using a
database in this article:
http://www.aspfaq.com/show.asp?id=2491

HTH,
Bob Barrows
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"


Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.