Count the total number of visitors for website

Karthikreddy
Posted by Karthikreddy under ASP.NET category on | Points: 40 | Views : 3430
1. Solution Explorer->Right click->add->new item->Global application class (Global.asax)

2.write below code under Global application class(Global.asax)


<%@ Application Language="C#" %>

<script runat="server">

void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["hits"] = 0;
}

void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
Application["this"] = Application["this"];

}

void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs

}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application["hits"] = Int32.Parse(Application["hits"].ToString())+1;
}

void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.

}

</script>


3. Add one empty web form and write the following code in code behind


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "total visiters " + Application["hits"].ToString();
}

}
[/code]

4. And run the application and copy the url and paste it on diff browsers

5. Stop the application and run it again the count will continue from precious count

Comments or Responses

Login to post response