12,955,891 members (66,546 online)
Rate this:
Please Sign up or sign in to vote.
See more:
How can I make a counter which counts the number of visitors?
Posted 26-Oct-10 21:26pm
moon20111.1K
Updated 27-Oct-10 3:42am
v3
Comments
Sandeep Mewara 27-Oct-10 4:11am

Surely a web thing, right?

## 3 solutions

Rate this:
Please Sign up or sign in to vote.

## Solution 3

Hi,

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

Steps:

1. Declare Variables

```private static int totalNumberOfUsers = 0;
private static int currentNumberOfUsers = 0;```

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

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

3. Session Start Event

```protected void Session_Start(Object sender, EventArgs e)
{

totalNumberOfUsers += 1;
currentNumberOfUsers += 1;

}```

4.Session End Event

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

}```

5.Access variables from other page

```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
Rate this:
Please Sign up or sign in to vote.

## Solution 4

For web application, try:
Write code in Global.asax file which interacts with the entire application.
The sample code is given below:
```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();
}```
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?
Rate this:
Please Sign up or sign in to vote.

## Solution 2

Go through the below link .., this may help you

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

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

Top Experts
Last 24hrsThis month
 OriginalGriff 325 Kornfeld Eliyahu Peter 270 CHill60 60 omerkamran 55 F-ES Sitecore 50
 OriginalGriff 6,719 CHill60 3,550 Maciej Los 3,153 ppolymorphe 2,060 Jochen Arndt 1,975

Advertise | Privacy | Mobile
Web01 | 2.8.170525.1 | Last Updated 27 Oct 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100