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 21:26pm
moon20111.1K
Edited 27-Oct-10 3: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
Your Filters
Interested
Ignored
     
0 Shai Vashdi 456
1 OriginalGriff 183
2 Sergey Alexandrovich Kryukov 171
3 Tadit Dash 160
4 praveen_07 115
0 Sergey Alexandrovich Kryukov 9,280
1 OriginalGriff 5,473
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 27 Oct 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid