Home | 2.0 Content | Code Samples | Tips and Tricks | -- Tutorials -- | ASP.Net Books | Resources | Hosting
The Best Place Yet for ASP.Net - ASPNet101.com
Power Search


Don't Miss any New Content!
Sign up for Newsletter!

  Menu  
  Code Samples
Tips and Tricks
Tutorials
ASP.Net FAQ
Training
Resources
Hosting
Recommended Books
Home
 
     
  Our Sponsors :  
 
ASP Express WebHost4Life DotNetSlackers
 
     

72
If you find this site helpful, please donate to help keep it online


     
 
   

Tracking & Displaying Sessions/Visitors Online
(How to show the current number of people at your site)

One of the more common questions I've found on the net is 'How do you show the number of current users browsing my site?", like ASPNet101.com has on the bottom left of each page. This tutorial will take the mystery out of this by breaking it down, piece by piece.

It all starts with the Global.asax file. For those of you who have come from the Classic ASP world, you will probably recognize the name of this file, since Classic ASP also had a Global.asa file. Here, we track the active Sessions for our application. There are three subroutines into which we'll be looking to do this - Application_OnStart, Session_OnStart, and the Session_OnEnd.

First, in the Application_OnStart sub, we basically set the user count to 0, when the server starts

Sub Application_OnStart (Sender as Object, E as EventArgs)
	' Set our user count to 0 when we start the server 
	Application("ActiveUsers") = 0
End Sub
Next, in the Session_OnStart subroutine, there are several things happening:
Sub Session_OnStart (Sender as Object, E as EventArgs)
	Session.Timeout = 10
	Session("Start") = Now 
	Application.Lock 
		Application("ActiveUsers") = Cint(Application("ActiveUsers")) + 1 
	Application.UnLock
End Sub
The first thing is the Timeout - you don't need to put anything here, but, the default Timeout is 20 minutes, so you can change or add it, depending on the needs of your particular application.

To set the session start time, we add (Session("Start") = Now). Basically, when the user hits the site and opens a web page (asp.net), at the time he/she opens the page, the session starts. Next, we increase the active visitors count when we start the session (Application("ActiveUsers") = Cint(Application("ActiveUsers")) + 1 ). The Application lock & unlock adds more stability to the counting.

Next, we must decrement the number of online sessions in the Session_OnEnd subroutine:

Sub Session_OnEnd(Sender as Object, E as EventArgs)
	Application.Lock 
		Application("ActiveUsers") = Cint(Application("ActiveUsers")) - 1 
	Application.UnLock
End Sub
As you can see, it's pretty much the same code as adding to the current session count - Except - we subtract one from the count, decreasing the active visitors count when the session ends.

The last thing to do is to display the count on the page. In this site, I have a user control that shows up on the left side of every page, which does this. Including it in a user control makes it much easier to maintain, since it only needs to be changed in one place and not on every page. As you can see here, very little code is actually necessary to show the current session/user count:

<% 
Dim intNumber as Integer
intNumber =Application("ActiveUsers")
response.write (intNumber ) 
%> Currently Online
Of course, you can use any html tags you'd like to format this in exactly the way you'd like for your site.

So now, you can see that the tracking and displaying the number of current sessions/users is a very simple process and can be implemented with little or no fuss on your own site.

   
Choose From Tutorials:
    Beginners Guide to Functions    
    Defining Error Displays Globally    
    Creating a Method to Return a DataSet in C#    
    Creating a Feedback Form    
    Beginners Guide to Comparing Strings    
    Introduction to Parsing Strings    
    Beginners Guide: Scoping of Methods    
    Adding 'Last Updated' To Footer    
    A Beginners Guide to the HyperinkField    
    Error Trapping in ASP.Net    
    Page Level Impersonation    
    Creating a Hit Counter    
    Inserting and Updating with a SQLDataSource    
    Sorting/Filtering Fixed DataSet Results    
    Using PreviousPage with a Master Page    
    The Basics of Using SQL    
    Helper Functions    
    Querystring Results and SQL Statements    
    A Beginners Guide to the Connection String    
    Beginners Guide to Com in ASP.Net    
    Membership/Roles with Remote DB    
    Creating a Login/Email/Activation Page    
    Multiple DropdownLists with AppendDataBoundItems    
    The Beginner's Guide to an ArrayList    
    Creating a Popup Details Page    
    Configuring a Trusted SQL Srvr Connection    
    Beginners Guide to the BulletedList    
    Adding Dynamic Content to Your Pages    
    Emailing Form Results with ASP.Net 2.0    
    Menu User Control with Rollovers    
    Using TemplateFields    
    Transferring Form Results to 2nd Page    
    Buttons and LinkButtons    
    Using the AdRotator Control    
    Two Page Data Retrieval    
    The 3 'Execute' Command Methods    
    Using the QueryStringParameter    
    Displaying DataBase Information    
    Beginner's Guide to Master Pages    
    Sessions/Visitors Online    
    Beginner's Guide to Themes    
    Sending Multiple Emails At Once    
    Sending Emails With ASP.Net 2.0    
    Emailing Form Results    
    A Beginner's Guide to the GridView    
    Inserting Data Into Two Tables    
    A Beginner's Guide to the DataSource Control    
    File System List With System.IO    
    Coolest New v2.0 Additions    
    Understanding Regular Expressions    
    Remote SQL Server/Web Matrix Server/WinXP Home    
    Parameterized Queries - Part 2    
    Creating a 'States' User Control    
    Creating an Online Bible    
    Iterating Through a List-Type Server Control    
    Nesting Server Controls    
    Beginner's Guide - Installing MSDE    
    String Properties/Methods - Part 2    
    Function Libraries    
    String Properties/Methods - Part 1    
    An Introduction to Validation Controls - Part I    
    Examining List Controls    
    Sending Mass Emails Part 2    
    If Not Page.IsPostBack    
    Beginners Guide to Forms Authentication    
    Single & Double Quotes in SQL    
    Using MySQL with ASP.NET    
    Parameterized Queries in ASP.Net    
    Matching Regular Expressions    
 
     
Home | 2.0 Content | Code Samples | Tips and Tricks | -- Tutorials -- | ASP.Net Books | Resources | Hosting
Send us your comments, questions or suggestions : Suggestions
Copyright © 2010 ASPNet101.com All Rights Reserved