Hello ,
I would like to count the no of hits to my website.
How do i do it?
whether I need to code it in Global.asax file in application on start ??????
Reply

Dani AI

Generated

For : the thread already has the common in-memory approach from (and the thumbs-up from ). is correct to warn that Application variables are ephemeral — they reset on app recycle and won’t survive process restarts — so that method is only suitable for quick demos or toy sites.

For a reliable production counter, persist counts outside the process (database or a distributed cache). Keep writes atomic on the server side; a simple pattern is an atomic increment in SQL (wrapped in a stored procedure or MERGE to handle the “insert-if-missing” case):

UPDATE HitCounters
SET Hits = Hits + 1
WHERE PageKey = @PageKey;

IF @@ROWCOUNT = 0
  INSERT INTO HitCounters (PageKey, Hits) VALUES (@PageKey, 1);

Avoid read-then-write from application code because it invites race conditions. For thread-safety inside a single process, use Interlocked.Increment or a concurrent dictionary and flush aggregates to persistent storage periodically.

For higher traffic or multi-server setups, batch increments in memory and write them in bulk on a timer (reduces DB load), or use a central store (SQL, Redis, etc.) so all nodes share counters. Note the tradeoff: batching reduces writes but can lose the last N seconds of data on a crash. Also avoid relying on Session_End for totals — it only fires for InProc sessions and is not reliable in farms.

Finally, clarify terminology and filtering: “hits” can mean raw resource requests, while “page views,” “sessions,” and “unique visitors” are different metrics. Filter bots/spiders (user-agent heuristics or analytics services) or parse IIS logs for historical accuracy if bot-filtered counts are required. This gives a more meaningful metric than a raw Application counter.

Recommended Answers

All 6 Replies

Check these links:


Hello ,
I would like to count the no of hits to my website.
How do i do it?
whether I need to code it in Global.asax file in application on start ??????
Reply

Hi you can place the code in Global.asax file and u need to call that variable in the aspx page. Example:

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

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    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.Lock();

        Application["Visitors"] = Convert.ToInt32(Application["Visitors"]) +1;
        Application.UnLock();
    }

    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.
        Application["Visitors"] = Convert.ToInt32(Application["Visitors"])-1;

    }

In the aspx file u need to call that variable as follow:

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

*** do reply whether it solves ur problem or not.....

use application variable....works... 100%

commented: bad advice -1

use application variable....works... 100%

Application variables are not persisted and you will lose the count data.

You can also take a look at a free application called smarter stats for weblog analysis. There is a lot more information to be had from IIS log files then the number of times your page was accessed. You have to take in to account spiders that crawl your site.

SmartStats: http://www.smartertools.com/

Thank you all ur support.
It works fine.

gr8 work...

Hi you can place the code in Global.asax file and u need to call that variable in the aspx page. Example:

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

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    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.Lock();

        Application["Visitors"] = Convert.ToInt32(Application["Visitors"]) +1;
        Application.UnLock();
    }

    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.
        Application["Visitors"] = Convert.ToInt32(Application["Visitors"])-1;

    }

In the aspx file u need to call that variable as follow:

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

*** do reply whether it solves ur problem or not.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.