Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Stardeveloper RSS Feed
Newsletter
Enter your email address below to be informed every time a new article is posted at Stardeveloper.com:

You can follow Faisal Khan on Twitter
Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (10)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Get Detailed Information About Your Site Visitors In Real Time using ASP.NET
 

Get Detailed Information About Your Site Visitors In Real Time using ASP.NET

by Faisal Khan.Follow Faisal Khan on Twitter

Overview
Have you ever wanted to see who is viewing your web site in real time? how many of them are there? what browser they are using? where have they come from? what are their host addresses and host names? and exactly what pages they have read? all in detail? Well, if you have wanted or want any of it, then keep reading.

What will we learn in this article?
Following are the topics covered:

  • How to count active users on a web site?
  • How to count all users who have accessed the web site since web site application started?
  • How to know what is the last time application restarted?
  • How to keep track of every user on the site?

What will we do in this article?
We'll be creating ( and/or modifying ) four files:

  1. viewSessions/default.aspx ( ASP.NET File )
  2. viewSessions/UserInfo.cs ( C# Source File )
  3. Global.asax ( ASP.NET Application File )

To compile UserInfo.cs source file, we'll make use of a .bat ( batch ) file.

  1. viewSessions/compile.bat

Once compiled, we'll have a /bin/Stardeveloper.Test1.dll assembly created in the "bin" folder.

To see how it'll look like when run, here is the screenshot when viewing viewSessions/default.aspx on my local system:

viewSessions/default.aspx
viewSessions/default.aspx ( Only part of the page is being displayed )

The table at the top will give information about how many active users are viewing your site in real time, how many have viewed it since application restart ( this will be greater than or equal to current sessions ) and what is the application restart time ( this can be useful if you want to know when your site administrator restarted the server ).

i. Global.asax
Let's start by creating the the "Global.asax" file. This is the main file for our application which is not displayed to the end user. Global.asax is an application events file for ASP.NET pages, pretty much like Global.asa was for ASP pages. This file contains application events which you may override to use for your own good.

Global.asax contains a lot more application events than original Global.asa file. If you are new and don't know what application events are, then application events are actions which occur in a web application like when application starts, when application stops, when a request is received, when a response is generated etc. What we can do is to put some code for these events if we want to make use of them e.g. if we want to note down the time our application started, we can make use of application started event.

For our session viewing application, we'll be using following events:

  1. Application_OnStart
    Called when application is started.

  2. Session_OnStart
    Called when a new user session is started.

  3. Session_OnEnd
    Called when an existing user session expires or is abandoned programmatically.

  4. Application_OnPostRequestHandlerExecute
    Called when the request has been processed by the Request Handler and HttpSessionState is available.

We'll be making use of above 4 application events. So create a new application ( Internet Services Manager -> Select your web site node in the left panel -> Right-click on it and select 'New' - 'Virtual Directory' -> Enter the 'Virtual Directory Alias' ( this will be your ASP.NET application alias e.g. 'dotnet' ) -> Enter the path to a physical directory on your hard disk ( this is where you'll be saving files for this project ) -> You are done ) or make use of an existing one.

Create or modify "Global.asax" file and make sure it contains the following code for four application events. If you already have code for some or all of these events in your Global.asax file, then you can modify your event methods to make room for our code.

Global.asax

<%@ Import namespace="Stardeveloper.Test" %>
<%@ Application Debug="true" %>
<script language="C#" runat="server">
  public void Application_OnStart(Object sender, EventArgs e)
  {
    Hashtable ht = (Hashtable)Application["SESSION_LIST"];
    if(ht == null)
    {
      ht = new Hashtable();
      lock(Application)
      {
        Application["SESSION_LIST"] = ht;
        Application["APP_START_TIME"] = DateTime.Now;
        Application["TOTAL_SESSIONS"] = 0;
      }
    }
  }
 
  public void Session_OnStart(Object sender, EventArgs e)
  {
    UserInfo ui = new UserInfo();
    Session["USER_INFO_MAP"] = ui;

    Hashtable ht = (Hashtable)Application["SESSION_LIST"];
    if(ht.ContainsKey(Session.SessionID) == false)
    {
      ht.Add(Session.SessionID, Session);
    }

    lock(Application)
    {
      int i = (int)Application["TOTAL_SESSIONS"];
      i++;
      Application["TOTAL_SESSIONS"] = i;
    }
  }

  public void Session_OnEnd(Object sender, EventArgs e)
  {
    Session.Clear();
    Hashtable ht = (Hashtable)Application["SESSION_LIST"];
    ht.Remove(Session.SessionID);
  }

  public void Application_OnPostRequestHandlerExecute(Object sender, EventArgs e)
  {
    try
    {
      UserInfo ui = (UserInfo)Session["USER_INFO_MAP"];
      if(ui != null && Session.IsNewSession)
      {
        try 
        {
          if(Request.UrlReferrer != null)
          {
            ui.URLReferrer = Request.UrlReferrer.ToString();
          }

          ui.UserAgent = Request.UserAgent;
          ui.HostAddress = Request.UserHostAddress;
          ui.HostName = Request.UserHostName;
        }
        catch (Exception)
        {
        }
      }

      if(ui != null)
      {
        ui.URLViews.Add(Request.RawUrl);
      }
    }
    catch(Exception)
    {
    }
  }
</script>

Explanation

<%@ Import namespace="Stardeveloper.Test" %>

First line is an 'Import' directive to import the 'Stardeveloper.Test' namespace. We are doing this to import 'UserInfo' class that we will create later in this article.

<%@ Application Debug="true" %>

This is an 'Application' directive and we are using it to set 'Debug' attribute to 'true', which means if something goes wrong and the compiler cannot compile this code then you'll be to see the exact line in your source code which caused that error.

<script language="C#" runat="server">
...
</script>

All the code for Global.asax resides between above <script></script> tags. The 'language' attribute specifies the language you have used to write code between these tags, which is C# in our case. The 'runat' attribute's value must be 'server' for our code to run on the server-side.

public void Application_OnStart(Object sender, EventArgs e)
{
	Hashtable ht = (Hashtable)Application["SESSION_LIST"];
	if(ht == null)
	{
		ht = new Hashtable();
		lock(Application)
		{
			Application["SESSION_LIST"] = ht;
			Application["APP_START_TIME"] = DateTime.Now;
			Application["TOTAL_SESSIONS"] = 0;
		}
	}
}

Overview
Have you ever wanted to see who is viewing your web site in real time? how many of them are there? what browser they are using? where have they come from? what are their host addresses and host names? and exactly what pages they have read? all in detail? Well, if you have wanted or want any of it, then keep reading.

What will we learn in this article?
Following are the topics covered:

  • How to count active users on a web site?
  • How to count all users who have accessed the web site since web site application started?
  • How to know what is the last time application restarted?
  • How to keep track of every user on the site?

What will we do in this article?
We'll be creating ( and/or modifying ) four files:

  1. viewSessions/default.aspx ( ASP.NET File )
  2. viewSessions/UserInfo.cs ( C# Source File )
  3. Global.asax ( ASP.NET Application File )

To compile UserInfo.cs source file, we'll make use of a .bat ( batch ) file.

  1. viewSessions/compile.bat

Once compiled, we'll have a /bin/Stardeveloper.Test1.dll assembly created in the "bin" folder.

To see how it'll look like when run, here is the screenshot when viewing viewSessions/default.aspx on my local system:

viewSessions/default.aspx
viewSessions/default.aspx ( Only part of the page is being displayed )

The table at the top will give information about how many active users are viewing your site in real time, how many have viewed it since application restart ( this will be greater than or equal to current sessions ) and what is the application restart time ( this can be useful if you want to know when your site administrator restarted the server ).

i. Global.asax
Let's start by creating the the "Global.asax" file. This is the main file for our application which is not displayed to the end user. Global.asax is an application events file for ASP.NET pages, pretty much like Global.asa was for ASP pages. This file contains application events which you may override to use for your own good.

Global.asax contains a lot more application events than original Global.asa file. If you are new and don't know what application events are, then application events are actions which occur in a web application like when application starts, when application stops, when a request is received, when a response is generated etc. What we can do is to put some code for these events if we want to make use of them e.g. if we want to note down the time our application started, we can make use of application started event.

For our session viewing application, we'll be using following events:

  1. Application_OnStart
    Called when application is started.

  2. Session_OnStart
    Called when a new user session is started.

  3. Session_OnEnd
    Called when an existing user session expires or is abandoned programmatically.

  4. Application_OnPostRequestHandlerExecute
    Called when the request has been processed by the Request Handler and HttpSessionState is available.

We'll be making use of above 4 application events. So create a new application ( Internet Services Manager -> Select your web site node in the left panel -> Right-click on it and select 'New' - 'Virtual Directory' -> Enter the 'Virtual Directory Alias' ( this will be your ASP.NET application alias e.g. 'dotnet' ) -> Enter the path to a physical directory on your hard disk ( this is where you'll be saving files for this project ) -> You are done ) or make use of an existing one.

Create or modify "Global.asax" file and make sure it contains the following code for four application events. If you already have code for some or all of these events in your Global.asax file, then you can modify your event methods to make room for our code.

Global.asax

<%@ Import namespace="Stardeveloper.Test" %>
<%@ Application Debug="true" %>
<script language="C#" runat="server">
  public void Application_OnStart(Object sender, EventArgs e)
  {
    Hashtable ht = (Hashtable)Application["SESSION_LIST"];
    if(ht == null)
    {
      ht = new Hashtable();
      lock(Application)
      {
        Application["SESSION_LIST"] = ht;
        Application["APP_START_TIME"] = DateTime.Now;
        Application["TOTAL_SESSIONS"] = 0;
      }
    }
  }
 
  public void Session_OnStart(Object sender, EventArgs e)
  {
    UserInfo ui = new UserInfo();
    Session["USER_INFO_MAP"] = ui;

    Hashtable ht = (Hashtable)Application["SESSION_LIST"];
    if(ht.ContainsKey(Session.SessionID) == false)
    {
      ht.Add(Session.SessionID, Session);
    }

    lock(Application)
    {
      int i = (int)Application["TOTAL_SESSIONS"];
      i++;
      Application["TOTAL_SESSIONS"] = i;
    }
  }

  public void Session_OnEnd(Object sender, EventArgs e)
  {
    Session.Clear();
    Hashtable ht = (Hashtable)Application["SESSION_LIST"];
    ht.Remove(Session.SessionID);
  }

  public void Application_OnPostRequestHandlerExecute(Object sender, EventArgs e)
  {
    try
    {
      UserInfo ui = (UserInfo)Session["USER_INFO_MAP"];
      if(ui != null && Session.IsNewSession)
      {
        try 
        {
          if(Request.UrlReferrer != null)
          {
            ui.URLReferrer = Request.UrlReferrer.ToString();
          }

          ui.UserAgent = Request.UserAgent;
          ui.HostAddress = Request.UserHostAddress;
          ui.HostName = Request.UserHostName;
        }
        catch (Exception)
        {
        }
      }

      if(ui != null)
      {
        ui.URLViews.Add(Request.RawUrl);
      }
    }
    catch(Exception)
    {
    }
  }
</script>

Explanation

<%@ Import namespace="Stardeveloper.Test" %>

First line is an 'Import' directive to import the 'Stardeveloper.Test' namespace. We are doing this to import 'UserInfo' class that we will create later in this article.

<%@ Application Debug="true" %>

This is an 'Application' directive and we are using it to set 'Debug' attribute to 'true', which means if something goes wrong and the compiler cannot compile this code then you'll be to see the exact line in your source code which caused that error.

<script language="C#" runat="server">
...
</script>

All the code for Global.asax resides between above <script></script> tags. The 'language' attribute specifies the language you have used to write code between these tags, which is C# in our case. The 'runat' attribute's value must be 'server' for our code to run on the server-side.

public void Application_OnStart(Object sender, EventArgs e)
{
	Hashtable ht = (Hashtable)Application["SESSION_LIST"];
	if(ht == null)
	{
		ht = new Hashtable();
		lock(Application)
		{
			Application["SESSION_LIST"] = ht;
			Application["APP_START_TIME"] = DateTime.Now;
			Application["TOTAL_SESSIONS"] = 0;
		}
	}
}

 ( 3 Remaining ) Next

See all comments and questions (post-ad) posted for this tutorial.


Comments/Questions ( Threads: 3, Comments: 4 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. not tracking page other than aspx ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.
  2. Problem passing data
  3. It wont run

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed to.

 
© 1999 - 2009 Stardeveloper.com, All Rights Reserved.