Count of current visitors

Discussion in 'ASP General' started by cc@cc.com, Aug 2, 2004.

  1. Guest

    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>
     
    , Aug 2, 2004
    #1
    1. Advertising

  2. John Guest

    Did you refresh your 1° page after closing 2 and 3 ?
     
    John, Aug 2, 2004
    #2
    1. Advertising

  3. 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"
     
    Bob Barrows [MVP], Aug 2, 2004
    #3
  4. Samuel Hon Guest

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

    wrote in message news:<>...
    > 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>
     
    Samuel Hon, Aug 2, 2004
    #4
  5. Larry Bud Guest

    > > 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.
     
    Larry Bud, Aug 2, 2004
    #5
  6. Guest

    On Mon, 2 Aug 2004 06:29:21 -0400, "Bob Barrows [MVP]"
    <> 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.

    > 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"
    >
    >
     
    , Aug 2, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yvon

    Do You Need Visitors

    Yvon, May 26, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    446
  2. r dubey
    Replies:
    2
    Views:
    3,316
    ʹÃûÑï
    Dec 15, 2003
  3. meltedown
    Replies:
    9
    Views:
    1,054
    Steve Pugh
    Oct 17, 2006
  4. Replies:
    1
    Views:
    598
    sloan
    Nov 18, 2008
  5. efelnavarro09
    Replies:
    2
    Views:
    976
    efelnavarro09
    Jan 26, 2011
Loading...

Share This Page