how to find number of online users

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • srj87
    New Member
    • Sep 2012
    • 4

    how to find number of online users

    i have an asp.net website which now i need to find the number of online users. i found couple of articles which suggest creating an application variable in global.asax file and increment it in session start event and decrement it on session end event. however with this approach im not able to get an accurate count and after 10 or 20 minutes the counter is reseted automatically. my website is in shared hosting environment and session config is stateserver any help is much appreciated...
  • Mudassir
    New Member
    • May 2012
    • 85

    #2
    i would suggest you to achieve this using your database. i.e take a field in your table and every time a user log in or logout, increment or decrement that field
    ...
    Addan

    Comment

    • srj87
      New Member
      • Sep 2012
      • 4

      #3
      by implementing this method i still wont get the actual number of users ie guest users. since this is an e-com website i am forcing users to login only after the checkout process. i am looking for something that would give approximate count of both guest and logged in users..

      Comment

      • Mudassir
        New Member
        • May 2012
        • 85

        #4
        then you should use the Glabal.asax like
        the Global.asax class may contain
        Code:
        <script runat="server">
        
          public static int count = 0;
          void Application_Start(object sender, EventArgs e) 
          {
            Application["myCount"] = count;
          }
        
          void Session_Start(object sender, EventArgs e) 
          {
            count = Convert.ToInt32(Application["myCount"]);
            Application["myCount"] = count + 1;
          }
        
        </script>
        and on aspx page you can have
        Code:
        protected void Page_Load(object sender, EventArgs e)
        {
          int a;
          a = Convert.ToInt32(Application["myCount"]);
          Label4.Text = a.ToString("0000");
          if (a < 10)
            Label4.Text = "000" + Label4.Text ;
          else if(a<100)
            Label4.Text = "00" + Label4.Text;
          else if(a<1000)
            Label4.Text = "0" + Label4.Text;
        }
        ...
        Addan

        Comment

        • srj87
          New Member
          • Sep 2012
          • 4

          #5
          i actually did something like that before and i am decrementing the counter on session end event in global.asax file code below
          Code:
          protected void Session_End(object sender, EventArgs e)
          {
                count = Convert.ToInt32(Application["myCount"]);
                Application["myCount"] = count - 1;
          }
          but if i open the page in different browsers the counter is certainly incrementing but its not decrementing once the browser is closed.

          Comment

          • srj87
            New Member
            • Sep 2012
            • 4

            #6
            if you want to check out the website here is the url

            Comment

            • ayukawaa
              New Member
              • Sep 2012
              • 9

              #7
              You have to have something on the database updated everytime the user ask for a page.

              It could be a counter for the pages shown, or anything you want

              In my case, I have the sessions stored in the database, and they are updated on every request so you only have to SELECT COUNT()... FROM SESSIONS WHERE the time is now minus 10 minutes...

              Comment

              Working...