article

An Easy Hit Counter

Email
Submitted on: 1/5/2015 4:31:00 PM
By: Primoris Software (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: ASP (Active Server Pages), HTML, VbScript (browser/client side)
Views: 1163
 
     This will give you the absolute basics on adding a hit counter to your web pages that reads information from a database table--not a file. This doesn't use pre-written objects, and is handled completely by the server.

 
				

Adding an Easy Hit Counter

This is the easiest way to add a hit counter that I know of that doesn't use the a premade component (like Microsoft's). This hit counter saves the hit information to a database instead of a file. This relies heavily on the global.asa file. If you're not familiar with the file, I suggest reading up on it a bit (I'm sure there's some good tutorials on the web). For our purposes here, just make sure you put this code in the global.asa in the root of your web, and if you have no global.asa file, it should be okay to create a new one with these two subprocedures in it. Be sure to include the <SCRIPT> tags.

As for the counter, the idea is simple. At the beginning of your application, load the previously saved hit values from the database. Do this by creating a quick connection, loading the values from a recordset, and then putting the values into application-wide variables. So your code might look like the following:

Sub Application_OnStart

Dim adoApp, adoSessConn
Set adoSessConn = Server.CreateObject("ADODB.Connection")
Set adoApp = Server.CreateObject("ADODB.Recordset")
adoSessConn.Open "DSN"
adoApp.Open "hits", adoSessConn, 2, 2, 2
adoApp.MoveFirst
Application.Lock
Application("Home_Hits")= adoApp("homehits")
Application.Unlock
adoApp.Close
adoAppConn.Close
Set adoApp = Nothing
Set adoAppConn = Nothing

End Sub

Now you've got one application variable loaded with the value from the last time it was saved--which, if you coded it right, will be the number of hits that page had received at the time the application ended. On our web site we keep track of about 30 pages this way, though only one is shown here.

Now every time a page is hit (or in this case we'll keep track of when it's refreshed, too) you will have a chunk of code at the beginning to increment that variable. Notice we do not have code to save this incremented value on every page. Why bog the server down when we only need it saved every so often? You can put code to write the values to a table whereever you want, naturally. Our system has been running for quite some time like this, however.

So now for the chunk of code on each page. It should look somewhat like the following:

<%

Application.Lock
Application("Home_Hits") = Application("Home_Hits") + 1
Application.Unlock

%>

Or for the adventurous, you can change the language of the page to JavaScript and use the trusty 'Application("Home_Hits") += 1;'. But you get the point. Now the only thing left to do is save the values to your database when the application ends. This would look like:

Sub Application_OnEnd

Dim adoApp, adoSessConn
Set adoSessConn = Server.CreateObject("ADODB.Connection")
Set adoApp = Server.CreateObject("ADODB.Recordset")
adoSessConn.Open "DSN"
adoApp.Open "hits", adoSessConn, 2, 2, 2
adoApp.MoveFirst
adoApp("homehits") = Application("Home_Hits")
adoApp.Update
adoApp.Close
adoAppConn.Close
Set adoApp = Nothing
Set adoAppConn = Nothing

End Sub

It's as easy as that! Yes, I know you can use a simple connection object and execute statement, but we're going for uniformity here, and I try to avoid using SQL statements except for selecting queries to be on the safe side. Notice that we always locked the application before updating it--this is standard practice for obvious reasons. It would by no means be disastrous not to lock this on such a simple procedure, but you might as well get into the habit.


Other 5 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.