Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Count Online Visitor in ASP.Net & VB.Net

Posted by Viral Sarvaiya on July 28, 2009

Place the following code in Global.asax file. Here, we track the active Sessions for our web application. There are three subroutines into which we will be looking to do this – Application_OnStart, Session_OnStart, Session_OnEnd.

Step-1 :

In the Application_OnStart subroutine, we have to set the user count to 0, when the server starts the application.

Sub Application_OnStart (Sender as Object, E as EventArgs)
     ' Set user count to 0 when start the application
     Application("ActiveUsers") = 0
 End Sub

Step-2 :

In the Session_OnStart subroutine, we have to increament the Activeuser by 1:


Sub Session_OnStart (Sender as Object, E as EventArgs)
      Application.Lock
      Application("ActiveUsers") = Cint(Application("ActiveUsers")) + 1
      Application.UnLock
End Sub

In this case you have to set Timeout – you don’t need to put anything here, but the default Timeout is 20 minutes, so you can change 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 page), at the time of opening 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.

Step-3 :

we must decrement the number of Active Users on the basis 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

Step-4 :

Then you can place the following code to access the Application(”ActiveUsers”) in .aspx file

<%
Dim intNumber as Integer
intNumber =Application("ActiveUsers")
response.write (intNumber )
%> Currently Online
About these ads

2 Responses to “Count Online Visitor in ASP.Net & VB.Net”

  1. ponchin said

    cool !

  2. Braj said

    Thanks a lot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 134 other followers

%d bloggers like this: