Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

Online active users counter in ASP.NET

Rate me:
Please Sign up or sign in to vote.
3.30/5 (20 votes)
30 Sep 2008CPOL3 min read 159.3K   8.1K   48   22
With this tool which is written for ASP.NET, it is possible to count the number of online users, members, and guest users in websites.

Introduction

With this tool which is written for ASP.NET, it is possible to count the number of online users, members, and guest users in websites.

The tool installation is very simple and only takes a few minutes.

Step one - Add Reference

After downloading the attachment, you should add a reference to the project. If you know how to add references, please skip this step.

To add the reference, right click on your solution and select "Add Reference" from the menu. Then select the "OnlineActiveUsers.dll" file. Now the reference is added to the solution.

Step two - Change configuration

Open the "Web.config" file from your project (if this file is absent, right click on your project and from "Add new item", select "Web configuration file"). Then, add this code to the "web.config" file.

XML
<httpModules>
  <add name="OnlineActiveUsers" 
       type="OnlineActiveUsers.OnlineUsersModule"/>
</httpModules>

Note that this code should be placed between the "system.web" tags.

Step three - Add some code to global.asax

Open the "global.asax" file in your project (if this file is absent, right click on your project and from "Add new item", select "Global Application Class"). In the "session_end" event, add this code:

C#
OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.UpdateForUserLeave()

If the project is C#, the result should be like this:

C#
void Session_End(object sender, EventArgs e)
{
    OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.UpdateForUserLeave();
}

Now the tool will start to count user statistics.

Step four - Access the statistics

To get the statistics, refer to the "OnlineActiveUsers.OnlineUsersInstance.OnlineUsers" variable.

Here are some of its important properties:

  • UsersCount: the number of all online users.
  • GuestUsersCount: the number of online guest users. This property works only if you have used the SetUserOffline and SetUserOnline methods. These methods are explained below.
  • RegistredUsersCount: the number of online members. This property works only if you have used the SetUserOffline and SetUserOnline methods. These methods are explained below.

Guest users and members

It is necessary to write some code to count the guest users and members correctly. Here is the explanation:

The first state we consider is when a user has entered your site and is logging into the site. To implement the process of counting guest users and members, there are several ways, but here I am using a simple way with the standard Login control in ASP.NET. If you are not using this control, skip this paragraph.

Add this code in the LoggedIn event of the Login control:

C#
OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOnline(Login1.UserName)

For example, in C#, the code should be like this:

C#
protected void Login1_LoggedIn(object sender, EventArgs e)
{
    OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOnline(Login1.UserName);
}

With this code, we specify that the user is logged in and will be counted as a member.

In a general way, when you're not using the Login control, the only thing that you should do is call this method after authenticating the user.

C#
OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOnline(UserName)

The "UserName" parameter should be an authenticated user name.

The second state we use is when the user is already logged in and he is going to logout of his account. If you are using the "LoginStatus" control, add this code to the "LoggedOut" event:

C#
OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOffline(User.Identity.Name)

For example, in C#, the code should be like this:

C#
protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
{
    OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOffline(User.Identity.Name);
}

Here, "User.Identity.Name" is the logged in user name.

In a general way, use this method to count the user as a guest:

C#
OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOffline(UserName)

In this code, the "UserName" parameter should be a user name. This name is case sensitive.

After these processes, the "GuestUsersCount" and "RegistredUsersCount" properties will show the number of guest users and members correctly.

Other methods

  • IsOnline method
  • This method returns a user's online state. The input parameter is the user name which is case sensitive, and the result is true if the user is online; otherwise, the result is false.

  • GetLastActivity method
  • This method returns the user's last activity time. The input parameter is the user name which is case sensitive, and the result is a "DateTime" if the user is online; otherwise, the result is "null" in C# and "Nothing" in VB.NET.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Anonymous Proxy Anonymous Proxy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIt gives me HTTP Error Pin
Member 121939038-Dec-15 5:41
Member 121939038-Dec-15 5:41 
Questionfor those having issues with guest count Pin
huwr10-Jul-15 3:06
huwr10-Jul-15 3:06 
Questionmy vote Pin
amnk.info2-Oct-13 2:20
amnk.info2-Oct-13 2:20 
SuggestionVery easier built-in API! Pin
Javid H17-Mar-13 19:43
Javid H17-Mar-13 19:43 
QuestionDoes not work well with ajax supported site Pin
Jonas T8-Jul-12 20:02
Jonas T8-Jul-12 20:02 
GeneralMy vote of 1 Pin
Jonas T8-Jul-12 19:32
Jonas T8-Jul-12 19:32 
GeneralGood Work. Pin
Ness20994-Jun-12 12:06
Ness20994-Jun-12 12:06 
QuestionMedium Trust Pin
Eric Beck17-May-12 4:32
Eric Beck17-May-12 4:32 
Questionuseronline Pin
Member 458650617-Apr-12 22:20
Member 458650617-Apr-12 22:20 
Questionabout IIS7 Pin
uspatel10-Nov-11 23:46
professionaluspatel10-Nov-11 23:46 
QuestionAbout Dll that contains properties to count usres. Pin
uspatel24-Jul-11 2:54
professionaluspatel24-Jul-11 2:54 
QuestionHow to count all Page Request? Pin
Doan Quynh22-Feb-11 16:40
Doan Quynh22-Feb-11 16:40 
GeneralMy vote of 1 Pin
crouchie0416-Oct-10 12:58
crouchie0416-Oct-10 12:58 
Doesn't work

Better to use Membership
QuestionSession handling Pin
Automation_lab9-Jul-10 1:29
Automation_lab9-Jul-10 1:29 
QuestionGuestUsersCount remains Zero Pin
Afflatus21-Feb-10 5:15
Afflatus21-Feb-10 5:15 
AnswerRe: GuestUsersCount remains Zero Pin
abartoch4-Nov-12 23:40
abartoch4-Nov-12 23:40 
QuestionIsOnline method Pin
kadherb14-May-09 20:32
kadherb14-May-09 20:32 
Generali enabeled it but got security exc Pin
skypainter1020-Jan-09 23:23
skypainter1020-Jan-09 23:23 
GeneralRe: i enabeled it but got security exc Pin
SalarSoft22-Jan-09 19:50
SalarSoft22-Jan-09 19:50 
Generaloops not working Pin
skypainter1018-Jan-09 22:21
skypainter1018-Jan-09 22:21 
GeneralRe: oops not working Pin
SalarSoft20-Jan-09 3:28
SalarSoft20-Jan-09 3:28 
Generalgreat man i like you,(if this dll will continue to work on production lol .thanks any way) Pin
skypainter1018-Jan-09 5:35
skypainter1018-Jan-09 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.