Share on ...      [Facebook]     [Twitter]     [Reddit]     [Linkedin]     [Hacker News]       O

A Free PHP Webpage View Counter that Ignores Self-Identified Robots

submited by
Style Pass
2024-04-28 21:00:19

Share on ...   [Facebook]  [Twitter]  [Reddit]  [Linkedin]  [Hacker News]     

One of the problems with online searches for website self-hosting software is that almost everything that can be found is either designed for companies, or it is designed to entice selfhosters into giving up data on the visitors to their websites. I learned this within days of deciding that I wanted to create my first website. When I searched for software to count the number of times that visitors to my website would be viewing its pages, I was essentially told that I was too stupid to do this horribly difficult task myself and that I would have to either pay a company to do it for me or allow a company to extract whatever information it wanted about the visitors to my website in exchange for its help. Neither of these appealed to me. Fortunately, I was aware that counting views of a web page is not exactly rocket science, so I began writing my own code. Surprisingly, my initial PHP webpage-view-counter script consisted of only 25 lines of code (including 9 lines of comments and blank lines). Greedy companies had wanted all of my visitors' data in exchange for little more than I could accomplish with 25 lines of code!

After weeks of testing that turned into months, I realized that nuances were involved. The simple approach I had taken was to create a file with a single number, a counter, that was the total number of times a particular webpage had been viewed by visitors. Sometimes that number appeared to have been corrupted, and my code would respond by writing a zero to the file, effectively resetting the counter. At first, I did not understand why this was occurring, but I noticed that it rarely happened until a webpage began to be viewed thousands of times an hour. Eventually, I realized that the problem was caused by multiple visitors trying to access the same webpage at the same time. This is known as a "race condition". Because articles on my blog rarely receive thousands of views an hour, solving this problem well enough to handle that level of traffic took me a few years of testing. The fact that my web server consists of a Raspberry Pi 3B writing to a micro SD card with at most a 10 MB/s write speed helped to reveal the problem much sooner than if I had been using a much faster server--one of the many benefits of using hardware that many people would turn up their noses at. In hindsight, perhaps I should have sped up the process by setting up a test server and using my laptop to simulate a high traffic load.

Leave a Comment