Get Detailed Information About Your Site Visitors In Real Time using ASP.NETby Faisal Khan.
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:
- viewSessions/default.aspx ( ASP.NET File )
- viewSessions/UserInfo.cs ( C# Source File )
- Global.asax ( ASP.NET Application File )
To compile UserInfo.cs source file, we'll make use of a .bat ( batch ) file.
- 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 ( 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:
- Application_OnStart
Called when application is started.
- Session_OnStart
Called when a new user session is started.
- Session_OnEnd
Called when an existing user session expires or is abandoned programmatically.
- 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:
- viewSessions/default.aspx ( ASP.NET File )
- viewSessions/UserInfo.cs ( C# Source File )
- Global.asax ( ASP.NET Application File )
To compile UserInfo.cs source file, we'll make use of a .bat ( batch ) file.
- 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 ( 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:
- Application_OnStart
Called when application is started.
- Session_OnStart
Called when a new user session is started.
- Session_OnEnd
Called when an existing user session expires or is abandoned programmatically.
- 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;
}
}
}
|