Count of current visitors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cc@cc.com

    Count of current visitors



    From the popular website At this link:
    W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.


    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="vbscr ipt" runat="server">
    Sub Application_OnS tart
    Application("vi sitors")=0
    End Sub

    Sub Session_OnStart
    Application.Loc k
    Application("vi sitors")=Applic ation("visitors ")+1
    Application.UnL ock
    End Sub

    Sub Session_OnEnd
    Application.Loc k
    Application("vi sitors")=Applic ation("visitors ")-1
    Application.UnL ock
    End Sub
    </script>

    To display the number of current visitors in an ASP file:

    <html>
    <head>
    </head>
    <body>
    <p>
    There are <%response.writ e(Application(" visitors"))%>
    online now!
    </p>
    </body>
    </html>

  • John

    #2
    Re: Count of current visitors

    Did you refresh your 1° page after closing 2 and 3 ?


    Comment

    • Bob Barrows [MVP]

      #3
      Re: Count of current visitors

      cc@cc.com wrote:[color=blue]
      > 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?
      >[/color]
      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:


      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"


      Comment

      • Samuel Hon

        #4
        Re: Count of current visitors

        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:<410dd003. 84332156@news.d allas.sbcglobal .net>...[color=blue]
        > 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="vbscr ipt" runat="server">
        > Sub Application_OnS tart
        > Application("vi sitors")=0
        > End Sub
        >
        > Sub Session_OnStart
        > Application.Loc k
        > Application("vi sitors")=Applic ation("visitors ")+1
        > Application.UnL ock
        > End Sub
        >
        > Sub Session_OnEnd
        > Application.Loc k
        > Application("vi sitors")=Applic ation("visitors ")-1
        > Application.UnL ock
        > End Sub
        > </script>
        >
        > To display the number of current visitors in an ASP file:
        >
        > <html>
        > <head>
        > </head>
        > <body>
        > <p>
        > There are <%response.writ e(Application(" visitors"))%>
        > online now!
        > </p>
        > </body>
        > </html>[/color]

        Comment

        • Larry Bud

          #5
          Re: Count of current visitors

          > > I got this code, but it does not work on my server... I can open three[color=blue][color=green]
          > > 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?
          > >[/color]
          > 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.[/color]


          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.

          Comment

          • cc@cc.com

            #6
            Re: Count of current visitors

            On Mon, 2 Aug 2004 06:29:21 -0400, "Bob Barrows [MVP]"
            <reb01501@NOyah oo.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.
            [color=blue]
            >cc@cc.com wrote:[color=green]
            >> 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?
            >>[/color]
            >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"
            >
            >[/color]

            Comment

            Working...