How many online visitors are there?

Όνομα: *
Το email μου: *
Email παραλήπτη: *
Μήνυμα: *
Τα πεδία με έντονους χαρακτήρες είναι υποχρεωτικά.
Δεν έχετε συμπληρώσει όλα τα απαιτούμενα στοιχεία Το email που δώσατε δεν είναι σωστό

One thing you can add to your website in order to show that it is a respectful place to spend some time, is a visitor counter. A person knowing that there are more visitors viewing your site will feel more confortable to post your forum or listen to what you have to say.

 
online-visitors
 

Getting Started

Let's see what we have to do in order to create a visitor counter. When we get a request, a session is created between our server and the client. That is when we should increase the counter. On the other hand, when this session is dropped we decrease the counter. Sounds easy, but it can be tricky for a developer who is not accustomed to asp.net.
 
To store our counter we will use application state. Application state is a state management tool which stores values in the session from the moment our website is first requested a resource till it is dropped or restarted. That makes it perfect for our application. More info on application state and how to use it here.
 

The Global.asax

Now, we will take advantage of what the Global.asax file can offer as. Global.asax is an asp.net file where we can insert methods that will fire on exact moments of the application lifecycle, for example when the application starts, when an unhandled exception occurs or when a session is created.
 
We will make use of three methods:
 
  •    Application_Start is executed when the application starts.
  •    Session_Start is executed when a session starts (when a new visitor requests a resource).
  •    Session_End is executed when a session is terminated. This can happen when the session times out or the visitor closes the browser.
   
Here's the code
 
void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    //Initialize the counter
    Application["OnlineVisitors"] = 0;
}
 
void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started
    //Use this when running online visitors page
    //Increase counter
    //Lock and unlock the application
    Application.Lock();
    Application[["OnlineVisitors"] = (int)Application[["OnlineVisitors"] + 1;
    Application.UnLock();
}
 
void Session_End(object sender, EventArgs e) 
{
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised.
 
    //Use this when running online visitors page
    //Decrease counter
    //Lock and unlock the application
    Application.Lock();
    Application[["OnlineVisitors"] = (int)Application[["OnlineVisitors"] - 1;
    Application.UnLock();
}
 
That's the idea. When the application starts the counter is zero. When a visitor appears, it is increased by one. When a visitor leaves, it is decreased by one.
 
When accessing application values it's a good thing to lock and unlock the application using Application.Lock() and Application.UnLock().
 
One last thing. In order for the Session_End to be executed, you need to set the sessionState mode in your web.config to InProc, for example
 
<sessionState mode="InProc" cookieless="false" timeout="1" />
 
cookieless="false" states that that we will use cookies to store our sessionID and timeout="1" states that after one minute elapses, the session will be dropped.
 

Application

To apply the number of visitors in a web page we will need a simple aspx page where we will insert a literal and few lines of code. Let;s do this using C#.
 
Current visitors : <asp:Label runat="server" ID="VisitorNumberLblID" />
 
OnlineVisitors.aspx
 
protected void Page_Load(object sender, EventArgs e)
{
    //Get the number of visitors stored inside the application state
    int onlineVisitors = (int)Application["OnlineVisitors"];
 
    VisitorNumberLblID.Text = onlineVisitors.ToString();
}
 
OnlineVisitors.aspx.cs
 
Here's an instance of the result:    
 
Current visitors : 2
 

Conclusion

It is quite simple to create a visitor counter. The counter is stored in the application state. We will use the Global.asax file's methods to initialize, increase and decrease the counter in the proper time.

Πίσω στο BlogΠροηγούμενοΕπόμενο

Σχόλια


  • 16-07-2015, 21:09 μμ
    Adelino
    Δημοσιεύσεις: 1
    That's a brilliant answer to an interesting question

Γράψε το σχόλιό σου
Όνομα: