The top bar of my website contains a counter to track the number of current
visitors on the site. It is very simple to implement. Start by opening your
Global.asax.cs file.
1) Add a private int variable named m_ActiveUsers
2) Add a public int property named ActiveUsers
3) In the Application_Start function, initialize m_ActiveUsers
4) In the Session_Start function add 1 to the counter
5) In the Session_End function subtract 1 from the counter
public class Global : System.Web.HttpApplication
{
protected static int m_ActiveUsers;
public static int ActiveUsers
{
get { return m_ActiveUsers; }
}
protected void Application_Start(Object sender, EventArgs e)
{
Application.Lock();
m_ActiveUsers = 0;
Application.UnLock();
}
protected void Session_Start(Object sender, EventArgs e)
{
Application.Lock();
++m_ActiveUsers;
Application.UnLock();
}
protected void Session_End(Object sender, EventArgs e)
{
Application.Lock();
--m_ActiveUsers;
Application.UnLock();
}
6) In your .aspx page include a label
called Visitors
7) In your .aspx.cs Page_Load function include the following line:
Visitors.Text = Global.ActiveUsers.ToString();