Howto Create a Hit Counter Using the Global.asa File
This article will show you how to accomplish these two tasks by storing the hit counters in Application variables using code in the global.asa file. The disadvantage of this method is that this information is lost when you restart the Web server. Two subsequent articles will demonstrate how to store this information in a text file and in a database, so the value for the counter will be preserved when you restart your Web server.
There is also an ASP.NET version of this article available.
PrerequisitesThe 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 UsersOne 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:
Application("TotalNumberOfUsers") = _
Application("TotalNumberOfUsers") + 1
Application("CurrentNumberOfUsers") = _
Application("CurrentNumberOfUsers") + 1
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("CurrentNumberOfUsers") = _
Application("CurrentNumberOfUsers") - 1
Testing it OutTo 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:
Current visitors: <%=Application("TotalNumberOfUsers")%>
SummaryThis 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.
- Howto Create a Hit Counter Using a Text File (http://Imar.Spaanjaars.Com/164/howto-create-a-hit-counter-using-a-text-file)
- Howto Create a Hit Counter Using a Database (http://Imar.Spaanjaars.Com/165/howto-create-a-hit-counter-using-a-database)
- When Sessions End - Once And For All! at www.asp101.com (http://www.asp101.com/articles/john/sessionsend/default.asp)
- Howto Create a Hit Counter Using the Global.asax File in ASP.NET (http://Imar.Spaanjaars.Com/223/howto-create-a-hit-counter-using-the-globalasax-file-in-aspnet-1x)
- ASP Session Object (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/050e0924-fd83-45e7-b105-50710538d6c1.asp)
- ASP Application Object (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/925aacf6-ea4f-423a-984c-414a68d91f1f.asp)
- How to Configure an ASP Web Application (and Enable Session State) (http://support.microsoft.com/?kbid=324278#4)
- Source Code for this Article (http://Imar.Spaanjaars.Com/Downloads/Articles/HitCounterInApplication.zip)
Where to Next?
Wonder where to go next? You can post a comment on this article.
Links in this Document
Talk Back! Comment on Imar.Spaanjaars.Com
I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.
When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.
If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the Doc ID of the document.
Please correct the following errors:
Unfortunately, something went wrong and your message or comments have not been submitted successfully.
There's a fair chance things broke down because you tried to post something that looks like HTML. Things that look like HTML include (X)HTML, obviously, XML, ASP.NET markup and c# generics syntax as all of them use the < and > characters.
If that's the case, try altering your message and remove anything that looks like an angled bracket. You can replace them with [ and ] for example so you can still make it look like HTML to some extend.
If, on the other hand, you were trying to spam this web site, I am pretty glad I caught you in the act and stopped you from doing so ;-)
Also, please don't use links in your posts; I had to block them to filter out most of the junk mail I am receiving.
The number you entered is not correct. Please enter the sum of the two numbers again.