23 May 2013

Count the Number of Online Visitors of an ASP.NET Web Site

Steps 1:

Create a web site for which you want to count online visitors.

Steps 2:

Add global.asax file into our website .Here is steps to add global.asax file into your website
  • Write Click on your website say Add new items

    Asp.net1.gif
     
  • In the item window select Global Application Class

    Asp.net2.gif
     
  • Click on Add Button

Note: Don't change Global.asax file name.

Step 3:


Write this code into the Global.asax file:

Which look like:
<%@ Application Language="C#" %>
<script runat="server">     void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup        Application["OnlineUsers"] = 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["OnlineUsers"] = (int)Application["OnlineUsers"] + 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.Lock();
        Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
        Application.UnLock();
    }
</script>
This will allow us to, whenever a distant web visitor opens our website in his browser,
create a new session for him by incrementing our "OnlineUsers" variable in the global HttpApplicationState class instance.

Also when the user closes his browser or does not click on any links in our website, the session expires,
and our "OnlineUsers" global variable is decreased.

NOTE: we are using the Application.Lock and Application.Unlock methods to prevent multiple threads from changing this variable at the same time.

By calling Application.Lock we are receiving exclusive right to change the values in Application state.

But we must not forget to call Application.Unlock() afterwards.

Step 4:

In order for this to work correctly we need to enable sessionstate and configure its mode to InProc value (in our web.config file):
        <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.

A 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 the timeout is set to 20 minutes, so while our visitors click on some links in our website at least once in a 20 minute period, they are considered online.

If they do not open any pages on our website during 20 minutes, they are considered offline, and their session is destroyed, so our online visitor counter is decreased by 1.

Note: You can change the Session State time by using a timeout property. The default time for a session is 20 minutes.

Step 5:

To show the number of online visitors/users on your ASPX page you can use this code:
Online Visitors: <%= Application["OnlineUsers"].ToString() %>

Next you can put this code snippet in you UserControl, or inside Asp.Net AJAX UpdatePanel control, and
use a Timer to refresh it in regular intervals without refreshing the whole page or you can write the following line inside a HTML body tag. It will refresh your Page every 5 minutes.
<meta http-equiv="Refresh" content="300">

Step 6: Run the web site.

No comments:

Post a Comment