Howto Create a Hit Counter Using a Text File
This article will show you how to accomplish these two tasks by storing the hit counters in Application variables and in a text file using code in the global.asa file. This article extends the ideas from a previous article where the values of the counters were just stored in Application variables. By writing the counters to a file you can maintain their values, even when you restart the Web server.
There is also an ASP.NET version of this article available.
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. You'll also need to be able to write to a text file from within the global.asa file. This means that the account your Web site is running under (usually, IUSR_MachineName where MachineName is the name of your computer) needs sufficient permissions to write to the file. Finally, most Anti-virus software come with a "Script blocking" feature, that disables writing to text files from within ASP pages. If you find that your files are not written at all, you may need to disable this feature. Alternatively, read this article that explains how to store your counters in a database.Counting Users
Just as in the article where the counters were stored in just the Application object, you can make use of the Session_OnStart event, defined in the global.asa file to keep track of your users. 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, Session_OnStart will fire 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.
Besides storing these values in the Application object, you can also write them to a text file. When your server is restarted, code in the global.asa file will read in the old values from the text file, so your counters will continue with the values they had before the restart.
Let's take a look at how you can accomplish all 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 Notepad or in your favorite 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:
Sub Session_OnStart
End Sub
Sub Session_OnEnd
End Sub
</script>
Sub Session_OnStart
Application("TotalNumberOfUsers") = _
Application("TotalNumberOfUsers") + 1
Application("CurrentNumberOfUsers") = _
Application("CurrentNumberOfUsers") + 1
Application.Unlock
' Values have been increased. Now write them to a text file as well
Dim aFSO ' As FileSystemObject
Dim aFile ' As Scripting.TextStream
Set aFSO = Server.CreateObject("Scripting.FileSystemObject")
Set aFile = aFSO.CreateTextFile(Server.MapPath("/") & _
"\Counter.txt", True)
aFile.Write ("TOTAL" & vbTab & Application("TotalNumberOfUsers"))
aFile.Close
Set aFile = Nothing
Set aFSO = Nothing
The second block of code in the Session_OnStart event writes this value to a text file. The FileSystemObject is used to create the file, using its CreateTextFile method. This method expects two parameters: the name of the text file (created by using Server.MapPath and appending Counter.txt to it) and a boolean indicating whether the file can be overwritten if it already exists. In this example True is passed, so you'll end up with a new file each time the Session_OnStart event fires. This way, you can be sure you always have just a single line of text in this file, because the old one is overwriiten.
Once the file is created and opened, the Write method of the TextStream object is used to write the value of the counter to the text file. When that is done, the file is closed and all objects are set to Nothing to clean up resources.
Notice that just the value of the TotalNumberOfUsers counter is written to the text fiile. There is no need to store the current number of users; after all, when the Web server is restarted, there are no current users yet.
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:
End Sub
Sub Session_OnEnd
Application("CurrentNumberOfUsers") = _
Application("CurrentNumberOfUsers") - 1
Application.Unlock
</script>
Reading the Counter When the Web Server Starts
As long as the Web server keeps running, this code will run fine. For each new Session that is created, the counters are increased by one and the value of TotalNumberOfUsers is written to the file. If, however, the Web server stops unexpectedly, or your restart or reboot the Web server yourself, the values for TotalNumberOfUsers and CurrentNumberOfUsers are lost. But because the value for TotalNumberOfUsers has also been stored in a text file, it's easy to retrieve the counter again from that file when the Web server starts. To read in the value from the Counter.txt file, you'll need to add some code to the Application_OnStart event that is also defined in the global.asa. Add the following code to the end of the global.asa file, right before the closing </script> tag:' Web server is started. See if counter file exists,
' and if it does, read in its value:
Dim aFSO ' As FileSystemObject
Dim aFile ' As Scripting.TextStream
Dim Result
Dim ArrResults
Set aFSO = Server.CreateObject("Scripting.FileSystemObject")
If (aFSO.FileExists(Server.MapPath("/") & "\Counter.txt")) Then
' File exists, grab its contents.
Set aFile = aFSO.OpenTextFile(Server.MapPath("/") & "\Counter.txt")
Result = aFile.ReadLine()
If Len(Result) > 0 Then
ArrResults = Split(Result, vbTab)
If IsArray(ArrResults) Then
If UBound(ArrResults) = 1 Then
Application("TotalNumberOfUsers") = ArrResults(1)
End If
End If
End if
aFile.Close
Set aFile = Nothing
End If
Set aFSO = Nothing
End Sub
</script>
It would have been easier to leave out the TOTAL in the text file, so there is no need to split the line of text you retrieved from the file. I did include it in this example though, because it can be useful if you want to store multiple counters in the same text file. This way, you can save individual counters as TOTALUSERS, TOTALDOWNLOADS and so on all in the same file, while you can still see which value means what.
At the end, the file is closed and the objects are cleaned up again, just as in the code for the Session_OnStart event.
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:Current visitors: <%=Application("TotalNumberOfUsers")%>
Next, restart your Web server. If you are using IIS, you can choose Restart IIS... from the All Tasks context menu for your Web server in the IIS Management Console.
Open Counter.asp again. You'll see that currently you're the only user browsing the site, but the total number of users has maintained its value.
Summary
This article demonstrated how to create a hit counter that keeps track of the current and total number of users to your site. It stores these variables in Application state so they are available in each page in your Web site. It also stores the value for the total number of users in a text file so its value won't be lost when the Web server restarts.A disadvantage of using text files is that it's difficult to scale out your Web site to lots of users. Only one user has access to the file at any given moment, so if you have a real busy site, you'll soon run into problems.
Check out the following article to see how you can improve the counter page even further by using a database instead of a text file.
- Howto Create a Hit Counter Using a Database (http://Imar.Spaanjaars.Com/165/howto-create-a-hit-counter-using-a-database)
Related Articles
- When Sessions End - Once And For All! at www.asp101.com (http://www.asp101.com/articles/john/sessionsend/default.asp)
References
- 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)
- FileSystemObject (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/sgprogrammingfilesystemobject.asp)
- How to Configure an ASP Web Application (and Enable Session State) (http://support.microsoft.com/?kbid=324278#4)
Download Files
- Source Code for this Article
(http://Imar.Spaanjaars.Com/Downloads/Articles/HitCounterInTextFile.zip)
Where to Next?
Wonder where to go next? You can post a comment on this article.
Links in this Document
Doc ID | 164 |
Full URL | https://imar.spaanjaars.com/164/howto-create-a-hit-counter-using-a-text-file |
Short cut | https://imar.spaanjaars.com/164/ |
Written by | Imar Spaanjaars |
Date Posted | 10/04/2003 17:18 |
Date Last Updated | 02/19/2005 11:48 |
Date Last Reviewed | 12/08/2006 14:44 |
Comments
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.