Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a website and need to know haw many people are using it. actually i need a counter and set a max number for clients. for example if max number is 3, then 4th client couldn't load my website.
Posted

Here is the example of doing the same. i took it from Mr Brij's article so if it helps do vote that article too(link is below)

A classic example of Application variable can be to show the number of online user in a website. This can be done in the following steps:

Add an online counter variable ApplicationStart method of Global.asax file as:

C#
Application["OnlineCounter"] = 0;


So in this, a variable will be added when the application first starts and will be initialized to 0 as there will be no logged in user at that point of time.
Now as we know whenever a new user opens the website, a new session is created and Session_Start method of Global.asax is called. So we can increase the counter in this method as:

C#
void Session_Start(object sender, EventArgs e)
{
    // Code that runs when a new session is started
   if (Application["OnlineCounter"] != null)
    {
        Application.Lock();
        Application["OnlineCounter"] =
        ((int)Application["OnlineCounter"]) + 1;
        Application.UnLock();
    }
}

We should use the Locks, else we may get the wrong result because this may be updated at the same time and updated data is not correct. How: Let's say we currently have Application["OnlineCounter"] is 5 and at the same time, two sessions read the value 5 and make an increment to 6 and updated it. Application state as 6. So although two users are logged in, the counter is increased by one only. So to avoid this, we should use the locks.
So also at the time session ends, we should decrease it by one. As I already discussed, an event Session_End is fired whenever a session ends. So it can be done as:


C#
void Session_End(object sender, EventArgs e)
      {
          // Code that runs when a new session is started
          if (Application["OnlineCounter"] != null)
          {
              Application.Lock();
              Application["OnlineCounter"] =
          ((int)Application["OnlineCounter"]) - 1;
              Application.UnLock();
          }
      }


And this value can be accessed throughout the application at any point of time in the application as:

C#
if (Application["OnlineCounter"] != null)
            {
                int OnlineUsers = ((int)Application["OnlineCounter"]);
            }   


and this value can be used anywhere in the application.

P.S. link of the article: A Walkthrough to Application State[^]
 
Share this answer
 
Comments
Abolfazl Beigi 6-Jul-12 4:21am    
Thanks dear friend. very clear and helpfully.
Rahul Rajat Singh 6-Jul-12 4:22am    
You are most welcome. always ready and glad to help a fellow developer.

Don't forget to vote the original article. it is a very good article and we should appreciate it by giving it a 5.
Abolfazl Beigi 6-Jul-12 9:32am    
+5
Shemeer NS 6-Jul-12 6:09am    
+5
you can solve your problem by using Application State.
for more please see:-
A Walkthrough to Application State[^]

best of luck.
 
Share this answer
 
Comments
Abolfazl Beigi 6-Jul-12 4:22am    
Nice help. thanks a lot.
Raje_ 6-Jul-12 6:31am    
you are always welcome.
Write code in Global.asax file which interacts with the entire application.
The sample code is given below:

C#
void Application_OnStart(Object Sender, EventArgs E)
{
  Application["CurrentUsers"] = 0;
}
void Session_OnStart(object Sender, EventArgs E)
{
  Application.Lock();
  Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) + 1;
  Application.UnLock();
}
void Session_OnEnd(object Sender, EventArgs E)
{
  Application.Lock();
  Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) - 1;
  Application.UnLock();
}

You can update/make changes here as per your need.

Refer: How to show number of online users visitors[^]
 
Share this answer
 
Comments
Abolfazl Beigi 6-Jul-12 4:21am    
thank you so much dear Sandeep.
Sandeep Mewara 6-Jul-12 4:29am    
Welcome.
You can do this by tracking IP address, then storing and checking unique IP's for your time duration(as 1 day) or storing unique session ID/UserID for unique user (if there is any custom login features)

thanks
 
Share this answer
 
Comments
Abolfazl Beigi 6-Jul-12 4:24am    
Thank you Dear soumen. nice help.
SoumenBanerjee 6-Jul-12 7:18am    
Welcome and Thanks againg

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900