Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How can I make a counter which counts the number of visitors?
Posted
Updated 27-Oct-10 3:42am
v3
Comments
Sandeep Mewara 27-Oct-10 4:11am    
Surely a web thing, right?

Hi,

u can use Application level variables and make use of the events in Global.asax

Steps:

1. Declare Variables

C#
private static int totalNumberOfUsers = 0;
    private static int currentNumberOfUsers = 0;


2. Add Properties
C#
public static int TotalNumberOfUsers
{
  get
  {
    return totalNumberOfUsers;
  }
}

public static int CurrentNumberOfUsers
{
  get
  {
    return currentNumberOfUsers;
  }
}



3. Session Start Event

C#
protected void Session_Start(Object sender, EventArgs e)
{

  totalNumberOfUsers += 1;
  currentNumberOfUsers += 1;

}


4.Session End Event

C#
protected void Session_End(Object sender, EventArgs e)
{
  currentNumberOfUsers -= 1;

}


5.Access variables from other page

C#
int currentNumberOfUsers = HitCounters.Global.CurrentNumberOfUsers;
int totalNumberOfUsers = HitCounters.Global.TotalNumberOfUsers;


Refer http://imar.spaanjaars.com/223/howto-create-a-hit-counter-using-the-globalasax-file-in-aspnet-1x[^]


:thumbsup: Revert for further clarification
 
Share this answer
 
For web application, try:
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();
        }
 
Share this answer
 
Comments
Dalek Dave 27-Oct-10 5:05am    
Good Answer.
hongthai91 4-Jan-13 3:58am    
can i view count the number of visitors by images?
Go through the below link .., this may help you

http://www.15seconds.com/issue/021119.ht[^]
 
Share this answer
 

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