Click here to Skip to main content
11,412,833 members (75,736 online)
Click here to Skip to main content

Online active users counter in ASP.NET

, 30 Sep 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

<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:

OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.UpdateForUserLeave()

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

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:

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

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

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.

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:

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

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

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:

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)

Share

About the Author

SalarSoft
Web Developer
Anonymous Proxy Anonymous Proxy
No Biography provided

Comments and Discussions

 
Questionmy vote Pin
amnk.info at 2-Oct-13 3:20
memberamnk.info2-Oct-13 3:20 
SuggestionVery easier built-in API! Pin
Javid H at 17-Mar-13 20:43
memberJavid H17-Mar-13 20:43 
QuestionDoes not work well with ajax supported site Pin
Jonas T at 8-Jul-12 21:02
memberJonas T8-Jul-12 21:02 
GeneralMy vote of 1 Pin
Jonas T at 8-Jul-12 20:32
memberJonas T8-Jul-12 20:32 
GeneralGood Work. Pin
Ness2099 at 4-Jun-12 13:06
memberNess20994-Jun-12 13:06 
QuestionMedium Trust Pin
Eric Beck at 17-May-12 5:32
memberEric Beck17-May-12 5:32 
Questionuseronline Pin
Member 4586506 at 17-Apr-12 23:20
memberMember 458650617-Apr-12 23:20 
Questionabout IIS7 Pin
Uma Shankar Patel at 11-Nov-11 0:46
memberUma Shankar Patel11-Nov-11 0:46 
QuestionAbout Dll that contains properties to count usres. Pin
Uma Shankar Patel at 24-Jul-11 3:54
memberUma Shankar Patel24-Jul-11 3:54 
QuestionHow to count all Page Request? Pin
Doan Quynh at 22-Feb-11 17:40
memberDoan Quynh22-Feb-11 17:40 
GeneralMy vote of 1 Pin
crouchie1998 at 16-Oct-10 13:58
membercrouchie199816-Oct-10 13:58 
QuestionSession handling Pin
Automation_lab at 9-Jul-10 2:29
memberAutomation_lab9-Jul-10 2:29 
QuestionGuestUsersCount remains Zero Pin
Afflatus at 21-Feb-10 6:15
memberAfflatus21-Feb-10 6:15 
AnswerRe: GuestUsersCount remains Zero Pin
abartoch at 5-Nov-12 0:40
memberabartoch5-Nov-12 0:40 
QuestionIsOnline method Pin
kadherb at 14-May-09 21:32
memberkadherb14-May-09 21:32 
Generali enabeled it but got security exc Pin
skypainter10 at 21-Jan-09 0:23
memberskypainter1021-Jan-09 0:23 
GeneralRe: i enabeled it but got security exc Pin
SalarSoft at 22-Jan-09 20:50
memberSalarSoft22-Jan-09 20:50 
Generaloops not working Pin
skypainter10 at 18-Jan-09 23:21
memberskypainter1018-Jan-09 23:21 
GeneralRe: oops not working Pin
SalarSoft at 20-Jan-09 4:28
memberSalarSoft20-Jan-09 4:28 
Generalgreat man i like you,(if this dll will continue to work on production lol .thanks any way) Pin
skypainter10 at 18-Jan-09 6:35
memberskypainter1018-Jan-09 6:35 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150427.1 | Last Updated 30 Sep 2008
Article Copyright 2008 by SalarSoft
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid