http://www.ComputerBob.com/guides/a_fast_simple_php_mysql_web_site_counter.php
pixel

A Fast, Simple PHP/MySQL Web Site Counter

by ComputerBob

June 13, 2008

NOTE: This article originally appeared as an entry in ComputerBob's daily online Journal.

A few days ago, I began looking for a web site counter to replace Spidercount on this web site. Spidercount is a text-file-based counter that worked fine for me for the past several years, but it's no longer fast enough or robust enough for this web site's traffic. Several weeks ago, when this site started getting more than two thousand visits per day, Spidercount started to spit up several times each week — emptying its counter file and then displaying error messages on all of this site's pages, complaining that its counter file was empty.

So I found myself looking for a fast, simple PHP/MySQL-based counter. After installing and trying close to 30 of them, I replaced Spidercount with EP-Dev Counter, which worked just fine. But I wasn't totally happy with EP-Dev Counter it because it has several features that I don't want or need. To me, that means that every time EP-Dev Counter counts a visitor to one of my web pages, it runs several extra MySQL database queries to support the features that I don't want or need. And that means that it runs slower than it needs to, with more chance of errors. And yes, I know that running those extra database queries probably doesn't make any noticeable difference in its speed, but I'd still rather have it not run those queries. After studying EP-Dev Counter's PHP code, as well as the code of several other PHP/MySQL counters, I thought that maybe I should go in and pare down its code to remove the features that I don't need, but then I decided that it might be a better idea to try to build a simple counter from the ground-up.

So I went looking for something that could teach me how to create my own PHP/MySQL web page counter. I found about ten different articles on building a web site counter, but most of them were either poorly documented or their documentation was confusing. I guess I could probably have just cut and pasted their lines of code and at least a few of them would have worked, but I wanted to understand my counter's code, not just copy and paste it.

One of the best how-tos I found was Monitor Hits to your site and MORE.. Using PHP and SQL! I could understand much of its code, but, like the EP-Dev Counter, it has several features that I don't want; for example, it can display its count as either simple text or as graphic images. It's not for me, but it's worth taking a look at it if you're thinking of building your own web site counter and you want one that has several features.

Another good tutorial was Simple IP Counter Based On PHP & MySQL. It's simple enough that I could follow its logic and see how it works, and it outputs its count as text-only, but it runs database queries that track of each visitor's IP address, and I don't need or want that feature.

For my needs, the winner was About.com's Web Page Hit Counter. By copying and pasting its short, understandable code, I got a very fast, very simple, PHP/MySQL web site counter in just a few minutes. Then I changed its last line — the one that displays the count on a web page — from a "print" statement to an "echo" statement. That change probably doesn't make it run noticeably faster, but it makes it consistent with all of the other web site counter scripts that I looked at.

Once I confirmed that the About.com counter was working correctly, I used what I had learned at WebDevelopers.com and Webmaster-Talk to add commas to the digits that you see at the bottom of this site's home page.

For about 16 hours, I ran both the EP-Dev Counter and the About.com counter on this site, so I could compare their counts. The count that this site's visitors saw at the bottom of its home page was from the About.com counter — the EP-Dev Counter was right above it, but it was hidden. Both counters used the same MySQL database, but they used separate database tables. After over 3,000 visits, their counts were the same. So I removed the EP-Dev Counter from this site's pages, deleted its database tables, and kept using the About.com counter.