Tien Nguyen
Tien Nguyen

Reputation: 600

What is the right way to count total visitor in a website?

I'm facing with an old problem that it made me confuse very much. So I need your advice to make sure that I've been using the right way. My demand is to count the number of visitor in my website, so I've coded in Global.asax file:

void Application_Start(object sender, EventArgs e) 
{
    // Get total visitor from database
    long SiteHitCounter = 0;
    int CurrentUsers = 0;
    SiteHitCounter = MethodToGetTotalVisitorFromDatabase();
    Application["SiteHitCounter"] = SiteHitCounter;
    Application["CurrentUsers"] = CurrentUsers;
}

void Application_End(object sender, EventArgs e) 
{
    //  Update total visitor to database when application shutdown
    MethodToUpdateTotalVisitorToDatabase((long)Application["SiteHitCounter"]);
}

void Session_Start(object sender, EventArgs e) 
{
    // Increase total visitor and online user
    Application["SiteHitCounter"] = (long)Application["SiteHitCounter"] + 1;
    Application["CurrentUsers"] = (int)Application["CurrentUsers"] + 1;
}

void Session_End(object sender, EventArgs e) 
{
    // Decrease online user
    Application["CurrentUsers"] = (int)Application["CurrentUsers"] - 1;
}

Then, I used variable Application["SiteHitCounter"] and Application[CurrentUsers"] in another C# behind code file to show them on web page. The problem I'm facing is that the website can't show right total visitor number as in my database when I publish it to shared host.

I need your advice on this.

Thanks, Tien

Upvotes: 2

Views: 1845

Answers (3)

Jeroen
Jeroen

Reputation: 4023

A visitor is someone who requests 1 page. There is no way of knowing if they are 'on' your site after the request, like if they are currently reading your page.

The session starts at the first requested page and expires 20 minutes after, even if the user just requested 1 page in the 1st second of the session and then left.

So there is no real way of knowing how many visitors you have at a given moment.

You could create a list with visiting IP addresses and register the time of the visit. You can then expire those entries yourself with a timer after let's say 20 minutes. It would also void duplicate sessions coming from the same IP.

Upvotes: 0

Azhar
Azhar

Reputation: 20670

check the link..

Setting an Application("Counter") in the global.asax

You should lock the variable before update because Its shared now.

void Session_Start(object sender, EventArgs e) 
{
    // Increase total visitor and online user
    Application.Lock();

    Application["SiteHitCounter"] = (long)Application["SiteHitCounter"] + 1;
    Application["CurrentUsers"] = (int)Application["CurrentUsers"] + 1;

    Application.UnLock();
}

void Session_End(object sender, EventArgs e) 
{
    // Decrease online user
    Application.Lock();

    Application["CurrentUsers"] = (int)Application["CurrentUsers"] - 1;

    Application.UnLock();
}

and if you want to make it fair apply some check to ip so that no one person can make multiple sessions.

Upvotes: 1

matt-dot-net
matt-dot-net

Reputation: 4244

you cannot guarantee that the session end event will fire. also you should be calling application.lock to make sure there are no concurrency issues on updating the counter . Also, it is possible that the same person would create multiple sessions during a life of your application so you may want to add ip address checking to further enhance the accuracy

Upvotes: 2

Related Questions