En  Fa
softprojects.org
Discovering: Articles  > Web Development  > ASP.NET  > Online active users counter in ASP.NET
 

Articles

 

 

Tips and Tricks

 


 

Online active users counter in ASP.NET

Print version  

Online active users counter in ASP.NET

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

Installation

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 on "web.config" file.

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

Note that this code should be placed between "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();
}

After now the tool will count user statistics.

Step four - Access the statics:

To get the statistics, refer to "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 SetUserOffline and SetUserOnline methods. These methods are explained below.

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

Guest User and Members

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

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

Add this code in LoggedIn event of 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 the general way, when you're not using Login control, the only thing that you should do is calling this method after authenticating the user.

OnlineActiveUsers.OnlineUsersInstance.OnlineUsers.SetUserOnline(UserName)

The "UserName" parameter should be authenticated user name.

The second state is when the user is already logged in and he is going to logout 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);
}

The "User.Identity.Name" is the logged in user name.

In the 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 the guest users and the members correctly.

Other methods:

  • IsOnline method:

This method returns the user 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 user last activity time. The input parameter is the user name which is case sensitive, and the result is "DateTime" if the user is online; otherwise the result is "null" in C# and "nothing" in vb.net .

Attachments
OnlineActiveUsers.zip
Size : 20.00 KB  Downloads : 67
 
Details
      
Writer: Salar Khalilzadeh
Date Sent: 8/1/2008 10:04 AM
Last Update: 9/26/2008 3:44 PM
Views: 217
Votes: 2
Rating: 5.00 Points from 5.00 Points

Your Rating:
bookmark this
 

There is no comment for this topic.

Language:

Copyright © 2008 SoftProjects.org | About | Valid XHTML | CSS