store small amounts of data in the Application and Session objects – like the number of users on the site. C# Version.

We’re going to use the Application and Session objects to keep count of how many users are visiting our site.

First, we build our page which is going to use variables stored in the Application and Session objects. It might look something like this:

The Global.asax is where we access the Application and Session objects to initialize our variables when they both start. The Application object exists as long as the website is online; the Session object exists as long as there is a user visiting the site – every unique visitor gets their own unique Session ID.
Upon Application Start, we add a variable to the object, and set it to zero. Upon Session Start, we increment that variable by one. This will give us an idea of how many current visitors there are, because each user has their own Session. And finally, upon Session End, we decrement the same variable by one – indicating that a user left the site.

The Global.asax file should look something like:

The code-behind will look something like this:

Download Source Files