Count of visitors visiting the website in asp.net Using C#

Posted by Sonalgupta1229 under ASP.NET on 9/23/2013 | Points: 10 | Views : 14778 | Status : [Member] | Replies : 2
Hi,


I am developing a video blog for website . I want to implement counting number of visitor of video on the basis of system IP Address .Can u Please how to do work Count Number of Visitors in WebSite using ASP.Net and C#.
Please urgently needed.
Thanking You,

Regards,
Sonal Gupta




Responses

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
http://www.codeproject.com/Questions/189910/Count-of-visitors-visiting-the-website
http://stackoverflow.com/questions/1508529/count-number-of-visitors-in-website-using-asp-net-and-c-sharp
http://www.dotnetfunda.com/forums/thread10410-count-of-visitors-visiting-the-website-in-aspnet-using-csharp.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sonalgupta1229, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/23/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Step 1:
We can do this by using (or creating) a global.asax file.
Every time a session is started, you increment an integer, which holds the number of current visitors. Every time a session is ended, you subtract one from that integer. Next, you will have to use public properties for all pages to be able to retrieve the information.

public class Global : System.Web.HttpApplication

{
private static int totalNumberOfUsers = 0;
private static int currentNumberOfUsers = 0;


protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
totalNumberOfUsers = 0;
}




protected void Session_Start(Object sender, EventArgs e)
{
// Code that runs when a new session is started
totalNumberOfUsers += 1;
currentNumberOfUsers += 1;
}
protected 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.


currentNumberOfUsers -= 1;
}
protected void Application_End(Object sender, EventArgs e)
{ }


public static int TotalNumberOfUsers{ get { return totalNumberOfUsers; } }
public static int CurrentNumberOfUsers{ get { return currentNumberOfUsers; } }
}


Step 2:

Next, in your control, set the values of the global.asax file in the right labels.
private void Page_Load(object sender, System.EventArgs e){

int currentNumberOfUsers = HitCounters.Global.CurrentNumberOfUsers;
int totalNumberOfUsers = HitCounters.Global.TotalNumberOfUsers;
lblCurrentNumberOfUsers.Text = currentNumberOfUsers.ToString();
lblTotalNumberOfUsers.Text = totalNumberOfUsers.ToString();
}



Step 3:
In order for this to work correctly we need to enable sessionstate and configure its mode to InProc value (in our web.config file):
<!--[if !supportLineBreakNewLine]-->

<!--[endif]-->
<system.web>
<sessionState mode="InProc" cookieless="false" timeout="20" />
</system.web>


In-process mode stores session state values and variables in memory on the local Web server.
It is the only mode that supports the Session_OnEnd event that we used previously.

Timeout value (in minutes, not in seconds) configures how long our sessions are kept 'alive' - in other words
here we set how long our users can be inactive before considered Offline.

In this example timeout is set to 20 minutes, so while our visitors click on some links in our website at least once
in a 20 minutes, they are considered online

NOTE: For accurate counting of visitors.....
http://stackoverflow.com/questions/11389880/how-to-get-the-accurate-total-visitors-count-in-asp-net

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Sonalgupta1229, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response