Prerequisites
The code in this article uses Sessions in ASP, so you'll need to have them enabled on your server. See the References section at the end of this article for more information. You'll also need to have access to a file called
global.asa in the root of your site. If you run your own Web server, this is not a problem; you can simply create the file yourself. If you are using an ISP, you'll need to check with them if they support the use of the
global.asa file, as unfortunately, not all ISPs allow this.
Counting Users
One of the easiest ways to count individual users is in the
Session_OnStart event that you can define in the
global.asa file. This event is fired whenever a user requests the first page in your Web site. This way, you have the ability to count each unique visitor only once during their visit. As long as the Session remains active on the server, the user won't be counted again. After the Session has timed out (it will automatically time out after a certain interval when no new pages have been requested) or has been explicitly ended, a request to a page will create a new Session, and the user will be counted again.
To keep track of the total number of users that have visited your site since you started the Web server, you can increase a counter for each request a user makes. Let's call this counter
TotalNumberOfUsers. You can store that counter in a variable in Application state, so you can retrieve and display it on other pages in your site. You can also create a second counter, called
CurrentNumberOfUsers for example, that counts the number of active Sessions on your server. Just as with
TotalNumberOfUsers, you increase the value of this counter whenever a new Session is started. However, you should decrease its value again when the Session ends. so you can keep track of the number of users that are currently visiting your site.
Well, enough for the theory; let's take a look at how you can accomplish this.
Start by creating a file called
global.asa (note that the extension is different from ordinary ASP pages) and save it in the root of your Web site. Open the file in Dreamweaver, Notepad or in your favorite ASP / HTML editor, and make sure it's completely empty (it shouldn't contain any ASP or HTML tags). Add the following ASP code to the file:
<script language="vbscript" runat="server">
Sub Session_OnStart
End Sub
Sub Session_OnEnd
End Sub
</script>
This code defines the skeleton for the two methods that fire when a new Session is started, and when a Session ends. You'll need to add the code for the counters to these two Subs. To keep track of the total number of users and the current number of users, you'll need to add the following shaded lines of code that create two Application variables:
<script language="vbscript" runat="server">
Sub Session_OnStart
Application.Lock
Application("TotalNumberOfUsers") = _
Application("TotalNumberOfUsers") + 1
Application("CurrentNumberOfUsers") = _
Application("CurrentNumberOfUsers") + 1
Application.Unlock
End Sub
When a user starts a new Session by requesting a page in your site, the code in the
Session_OnStart event will fire. This code will lock the Application object temporarily, to prevent two users from writing to it at the same time. Then the Application variables
TotalNumberOfUsers and
CurrentNumberOfUsers are increased by one. Once they are increased, the Application is unlocked again, so it's available to other users.
To decrease the counter for the
CurrentNumberOfUsers, you'll need to add some code to the
Session_OnEnd event, that will fire when a Session times out, or when it is explicitly ended. You should just decrease the value for
CurrentNumberOfUsers, and leave
TotalNumberOfUsers untouched. Add the following shaded lines of code to the
Session_OnEnd event in your
global.asa file:
Application.Unlock
End Sub
Sub Session_OnEnd
Application.Lock
Application("CurrentNumberOfUsers") = _
Application("CurrentNumberOfUsers") - 1
Application.Unlock
End Sub
</script>
Whenever a Session is ended, the value of
CurrentNumberOfUsers is decreased by one. This way, its value will always reflect the number of users that are browsing your site right now.
Testing it Out
To test out your hit counter, create a new ASP file and call it
Counter.asp. You can save it anywhere in your site. Add the following ASP code to that page:
Total number of visitors: <%=Application("CurrentNumberOfUsers")%><br>
Current visitors: <%=Application("TotalNumberOfUsers")%>
These two lines of code write out the total number of visitors and the current number of visitors. Open the page in your browser and you'll see there is one current user. Also, note that the total number of users is 1. Open another browser (don't use
Ctrl+N, but start a fresh instance or use an entirely different brand of browser) and open the counter page. You'll see there are two current users, and two users in total. Wait until both the Sessions have timed out (the default timeout for the IIS Web server is 20 minutes) and open the hit counter page again. You'll see there is one current user, but the total number of users has been maintained and should be 3.
Summary
This short article demonstrated how to create a hit counter that keeps track of the current and total number of users on your site. It stores these variables in Application state so they are available in each page in your Web site. A big disadvantage of storing these variables in the Application is that their values get lost when the Web server is restarted. You should check out the following two articles to see how you can save the counter value for the total number of users in a text file or in a database. By saving the counter to a text file or database, its value can be maintained, even when you restart or reboot the Web server.
Related Articles
References
Download Files