Microsoft Dot Net Master

Microsoft Dot Net Master
Microsoft Dot Net Master

Thursday, September 1, 2011

Show Current User Count On Your Website

Web application contained a file called the "global.asax". The Global.asax file is event driven. It can contain four event procedures: Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd.

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["WhoOn"] = 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["WhoOn"] =Convert.ToInt32(Application["WhoOn"]) + 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["WhoOn"] =Convert.ToInt32( Application["WhoOn"]) - 1;
        Application.UnLock();

    }
      
</script>

Then to display the count on any ".aspx" page in your web site just do something like this.

<%= Application("WhoOn") %> USERS CURRENTLY ONLINE

Now, the Session_OnEnd part basically means that if a user has no activity for a period of time that (1) will be subtracted from the current user count. That time period being whatever you have the Session.Timeout set to. This means that if your Session.Timeout is set to 20 minutes that it may take up to 20 minutes for the count to update after a user leaves.

The other thing to realize is that a user can inflate the count by closing their browser and coming back to your site over and over. This problem takes care of it itself over time as all those counts will eventually expire and go away on their own but it is something to be aware of. Sometimes you'll notice this happen and it will be some jerk messing around to see if they can make your count go up. Other times it will be search engine bots indexing your site that cause the count to spike.

Also remember that if the web is not set up to run as an application the "global.asax" will not run. You'll need to make sure the web is an application. Most Virtual Domains are by default, but sub webs usually are not.

For the sub webs to run the 'global.asax" they need to be an application as the root usually is. In NT this is accomplished via the Internet Service Manager under the properties of the sub web you want to make an application

No comments:

Post a Comment