Section 4.10. Determine How Many People Are Currently Using Your Web Site

4.10. Determine How Many People Are Currently Using Your Web Site

The Web uses HTTP, a stateless protocol that rarely maintains a connection longer than a few seconds. As a result, even as users are reading through your web pages, they aren't connected directly to your server. However, ASP.NET gives you a way to estimate how many people are using your web site at any given moment using timestamps. This information makes a great addition to a community site (e.g., a web discussion forum), and it can also be useful for diagnostic purposes.

Note: Ever wondered how many people are using your site right now? If you're using ASP. NET's personalization features, it's remarkably easy to get a reasonable estimate.

4.10.1. How do I do that?

Every time a user logs in using a membership provider (described in the lab "Easily Authenticate Users"), ASP.NET records the current time in the data store. When the same user requests a new page, ASP.NET updates the timestamp accordingly. To make a guess at how many people are using your web site, you can count the number of users who have a timestamp within a short window of time. For example, you might consider the number of users who have requested a page in the last 15 minutes.

You can retrieve this information from ASP.NET using the new GetNumberOfUsersOnline() method of the Membership class. You can also configure the time window that will be used by setting the UserIsOnlineTimeWindow property (which reflects a number of minutes). It's set to 15 by default.

Here's a code snippet that counts the online users and displays the count in a label:

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)     lblStatus.Text &= "<br>There are " &_     Membership.GetNumberOfUsersOnline( ) & _       " users online right now. That is an estimate based" &_       " on looking at timestamps that fall in the last " &_       Membership.UserIsOnlineTimeWindow & _       " minutes." End Sub

Keep in mind that this count doesn't include anonymous users.

4.10.2. What about...

...getting information about exactly which users are online? Unfortunately, ASP.NET doesn't currently provide any way to determine which users are online. The only alternative is to add your own tracking code. For example, you could store this information in a database or add it to an in-memory object such as the Application collection whenever a user logs in. You would also need to store the login time and discard old entries periodically.

Visual Basic 2005(c) A Developer's Notebook
Visual Basic 2005: A Developers Notebook
ISBN: 0596007264
EAN: 2147483647
Year: 2006
Pages: 123

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: