Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Web
How can I make a counter which counts the number of visitors?
Posted 26-Oct-10 22:26pm
moon20111.1K
Edited 27-Oct-10 4:42am
v3
Comments
Sandeep Mewara at 27-Oct-10 4:11am
   
Surely a web thing, right?
Rate this: bad
good
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[^]
 

Thumbs Up | :thumbsup: Revert for further clarification
  Permalink  
Rate this: bad
good
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();
        }
  Permalink  
Comments
Dalek Dave at 27-Oct-10 5:05am
   
Good Answer.
hongthai91 at 4-Jan-13 3:58am
   
can i view count the number of visitors by images?
Rate this: bad
good
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[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 235
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411028.1 | Last Updated 27 Oct 2010
Copyright © CodeProject, 1999-2014
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