Search This Blog

Monday, November 5, 2012

Hit Counter for Total number of Current Visitors on website

Total No of Current Visitors :

For current visitors hit we can use Global.asax file and Application variable :

Global.asax :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Data.SqlClient;
using System.Text;
using System.IO;

namespace SRTT_HEALTH
{
    public class Global : System.Web.HttpApplication
    {

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

        protected void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            Application.Lock();
            Application["Visitors"] = Convert.ToInt32(Application["Visitors"]) + 1;
            int users = Convert.ToInt32(Application["Visitors"]);
            Application.UnLock();
         
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
         
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}

Other page code where show visitors number:


protected void Page_Load(object sender, EventArgs e)
        {
         
            Label1.Text=Application["Visitors"].ToString();
         
        }



No comments :