Count of current visitors

C

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>
 
B

Bob Barrows [MVP]

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
 
S

Samuel Hon

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

Larry Bud

I got this code, but it does not work on my server... I can open three
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.
 
C

cc

On Mon, 2 Aug 2004 06:29:21 -0400, "Bob Barrows [MVP]"

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top