Friday 11 January 2013

Get the Online Visitors Count in Your Website Dynamically


Get the Online Visitors Count in Your Website Dynamically


Write the following Code Snippet in your Global.asax file:

void Application_Start(object Sender, EventArgs E)

{

// Set our user count to 0 when we start the server

Application["ActiveUsers"] = 0;

}

void Session_Start(object Sender, EventArgs E)

{

Session["Start"] = DateTime.Now;

Session.Timeout = 1;

Application.Lock();

Application["ActiveUsers"] = (int)Application["ActiveUsers"] + 1;

Application.UnLock();

}

void Session_End(object Sender, EventArgs E)

{

Application.Lock();

Application["ActiveUsers"] = (int)Application["ActiveUsers"] - 1;

Application.UnLock();

Session.Clear();

Session.Remove("Start");

}

Now in the Design Part you can get this number by calling Application["ActiveUsers"] in the .aspx file...

For Example if you want to display the number of visitors online in a label, then do like this:


lblCount.Text=Application["ActiveUsers"].ToString(); 



No comments:

Post a Comment

Thank You for Your Comments. We will get back to you soon.

back to top