Hit Counter Using the Global.asax

Posted by Suneel161 under ASP.NET on 8/17/2010 | Points: 10 | Views : 8790 | Status : [Member] | Replies : 3
Howto Create a Hit Counter Using the Global.asax File in ASP.NET




Responses

Posted by: Shankul2784 on: 8/17/2010 [Member] Bronze | Points: 25

Up
0
Down
Hi,

use the below link-

http://www.eggheadcafe.com/community/aspnet/2/10050884/hits.aspx

or

http://forums.iis.net/t/1149173.aspx

or

http://stackoverflow.com/questions/2491179/about-global-asax-and-the-events-there


I hope, this will help you.

Thanks & Regards,

Shailesh

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saikrishna on: 8/17/2010 [Member] Starter | Points: 25

Up
0
Down
Locate the code that starts with public class Global : System.Web.HttpApplication and add the following shaded lines of code:
namespace HitCounters
{
/// <summary>
/// Summary description for Global.
/// </summary>
public class Global : System.Web.HttpApplication
{
private static int totalNumberOfUsers = 0;
private static int currentNumberOfUsers = 0;

/// <summary>
/// Required designer variable.
Whenever the Web application starts, the Global class is constructed and the two hit counters will be initialized to 0.

The next step is to add code to the Session_Start event. This event will fire once for each user when they request the first page in your Web application, so this place is perfect for your hit counter. Inside this event, the values of the two hit counters are increased; one for the total number of users and one for the current number of users.
Locate the skeleton for the Session_Start event and add the following code:
protected void Session_Start(Object sender, EventArgs e)
{
totalNumberOfUsers += 1;
currentNumberOfUsers += 1;
}
Just as with the Session_Start event, you'll need to write some code for the Session_End event. However, instead of increasing the counters, you should decrease the counter for the current number of users only. This means that whenever a user Session times out (usually 20 minutes after they requested their last page), the counter will be decreased, so it accurately holds the number of current users on your site. You should leave the counter for the total number of users untouched.
Locate the Session_End event, and add this code:
protected void Session_End(Object sender, EventArgs e)
{
currentNumberOfUsers -= 1;
}
Making Your Counters Accessible by Other Pages
Since the hit counters are stored in the Global class, you need some way to get them out of there, so you can display them on a management page for example. The easiest way to do this, is to create two public properties. Because the Global class is in many respects just an ordinary class, it is easy to add public properties to it.

To add the properties, locate the skeleton for the Application_End event, and add the following code right below it:

protected void Application_End(Object sender, EventArgs e)
{

}

public static int TotalNumberOfUsers
{
get
{
return totalNumberOfUsers;
}
}

public static int CurrentNumberOfUsers
{
get
{
return currentNumberOfUsers;
}
}
With these two read-only properties in place, your calling code is now able to access the values of your hit counters. For example, to retrieve the number of users browsing your site right now, you can use this code: HitCounters.Global.CurrentNumberOfUsers where HitCounters is the default namespace for your Web application as defined on the Property Pages for the Web project in Visual Studio .NET.
Testing it Out
To test out your hit counters, create a new Web form and call it HitCounter.aspx. You can save the form anywhere in your site. In Design View, add two labels to the page and call them lblTotalNumberOfUsers and lblCurrentNumberOfUsers respectively. Add some descriptive text before the labels, so it's easy to see what value each label will display. You should end up with something similar to this in Code View:

<body>
<form id="frmHitCounter" method="post" runat="server">
Total number of users since the Web server started:
<asp:label id="lblTotalNumberOfUsers" runat="server"></asp:label><br />
Current number of users browsing the site:
<asp:label id="lblCurrentNumberOfUsers" runat="server"></asp:label><br />
</form>
</body>
Press F7 to view the Code Behind for the hit counter page, and add the following code to the Page_Load event:

private void Page_Load(object sender, System.EventArgs e)
{
int currentNumberOfUsers = HitCounters.Global.CurrentNumberOfUsers;
int totalNumberOfUsers = HitCounters.Global.TotalNumberOfUsers;
lblCurrentNumberOfUsers.Text = currentNumberOfUsers.ToString();
lblTotalNumberOfUsers.Text = totalNumberOfUsers.ToString();
}

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajumm on: 8/17/2010 [Member] Starter | Points: 25

Up
0
Down
 void Session_Start(object sender, EventArgs e) 

{
if (Application["counter"] == null)
{
Application["counter"] = 0;
}
else
{
Application.Lock();
Application["counter"] = (int)(Application["counter"]) + 1;
Application.UnLock();
}
}


Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response