How to Count Number of Visitors for Website in ASP.NET C#

Count Number of Visitors in WebSite using ASP.Net and C#

If you want the count to keep incrementing over application restarts, you'll need to store the value somewhere - in a database or a file somewhere, and load that value up when the application starts.

Also, you can use the following to ensure your displayed count is always at least 4 characters:

int a;
a = Convert.ToInt32(Application["myCount"]);
Label4.Text = a.ToString("0000");

See Custom Numeric Format Strings for more info.


Edit to respond to comment

Personally, I'd recommend using a database over writing to the file system, for at least the following reasons:

  1. Depending on your host, setting up a database may well be a lot easier than enabling write access to your file system.
  2. Using a database will allow you to store it as an int rather than a string.
  3. Under heavy traffic, you'll have issues with multiple threads trying to open a text file for write access - which will cause a lock on the file, and cause a bottle neck you don't need.

Various resources will tell you how to connect to a database from your code, a good place to start would be this How To: Connect to SQL Server, and looking into the methods under "What are the alternatives" for details on how to query and update the database.

how to count number of visitors for website in asp.net c#

If your application is hosted in IIS and has an application pool, you can check the Application Pool Recycling Settings. Depending on your version, the default is 1740 or 29 hours. Maybe the pool for your application is configured to 60 or around that value? The next setting to check is the Idle Time Out. I believe its default value is 20 on a new server. You can set this to 0. I recommend you read about these settings prior to changing them.

How to get the accurate total visitors count in ASP.NET

You can never get an entirely accurate number: there is no way to (reliably) detect that a user "has navigated to another site" (and left yours) or that that user "closed the browser".

The Session_Start/Session_End way has the problem that Session_End is only called for "InProc" sessions, not if the sessions are stored in StateServer or SqlServer.

What you might be able to do:

  • Hold a Dictionary<string, DateTime> in Application scope. This stored session-id's (the string key) against the time of latest access (the DateTime value)
  • For each request with a valid session, find the session entry in the dictionary and update it's latest-access time (add a new entry if not found)
  • When you want to get the number of online users, first loop through all entries in the dictionary and remove items where the session timeout has passed. The remaining count is the number of online users.

One problem (at least): if one user uses two browsers simultaneously, he has two sessions open and is counted double. If users always log in, maybe you could count on login-id instead of session-id.

ASP.NET Visitors counter

Application_Start is runs only when process created - not every visit.

You can use Application_BeginRequest instead.

count daily visitor of a website in c#

    private void Form1_Load(object sender, EventArgs e)
{
// number of visitor
int totalNumbers = 0;
// Database
string dbConStr = "Server=.\\sqlexpress;Database=Test;Integrated Security=true;";
System.Data.SqlClient.SqlConnection sqlConnection1 =
new System.Data.SqlClient.SqlConnection(dbConStr);

System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = "INSERT CounterTable (totalNumbers, day) VALUES (@totalNumbers, getdate())";
cmd.Parameters.Add("totalNumbers",SqlDbType.Int).Value = totalNumbers;
cmd.Connection = sqlConnection1;

sqlConnection1.Open();
cmd.ExecuteNonQuery();
sqlConnection1.Close();
}

How can i count unique visitors on my website?

You could create a session cookie for this. Every time someone connects to your service you check for that cookie and increment the counter if the cookie does not exist yet.

Here's how you define a cookie, let's call it "hasVisited":

HttpCookie aCookie = new HttpCookie("hasVisited");
aCookie.Value = true;
aCookie.Expires = DateTime.Now.AddDays(100);
Response.Cookies.Add(aCookie);

You then read it like this:

if(Request.Cookies["hasVisited"] == null)
{
// increment counter and add cookie for future reference...
}

You could also work with IP and MAC-Addresses (being less reliable due to firewalls etc.). To get the IP of the client use:

var remoteIpAddress = Request.UserHostAddress;

For the MAC address I suggest you look at

http://www.dotnetfunda.com/forums/show/2088/how-to-get-mac-address-of-client-machine

for further information.

asp.net visitors count (online users) for today-yesterday in .net 4

The "standard" approach I have seen is to use the Session_Start and Session_End within global.asax. ASP.net 4 has not changed this functionality. The only real limitation to this approach is that the server will still believe that the user is logged on until the session ends, which does not happen until the number of minutes has passed as specified in the session timeout configuration.

See this page for more information about session timeout:
http://forums.asp.net/t/1283350.aspx

One more robust, and newer, but much more difficult approach, is to use polling to ensure that the client is always connected to the server. See this Wikipedia article for an overview of the subject:
http://en.wikipedia.org/wiki/Comet_(programming))

Implementing a visitor counter

Google analytic script is exactly what you need. Because session, will opens for crawlers too.



Related Topics



Leave a reply



Submit