Sunday, 13 January 2013 13:55

how to count how many visitors are online

Written by 
Rate this item
(0 votes)

n your asp.net application/website you can count
how many visitors are browsing your website easily.
for this add a Global.asax file in your website.
then add the following codes in that file -

<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["OnlineUsers"] = 0;
}
void Application_Error(object sender, EventArgs e)
{
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs whenever a new session starts
Application.Lock();
Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
// This Session_End event is raised when the
//sessionstate mode
// is set to InProc in Web.config file.
Application.Lock();
Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
Application.UnLock();
}
</script>

then add the following code in any page to show
how many visitors are watching your website -

Total Visitors online - <%= Application["OnlineUsers"].ToString() %>

Read 2784 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.