In my search for a simple PHP web counter it was difficult to find one that wasn’t somehow connected to an external host and didn’t write IP addresses to a file and then use resources to search the file later. With all the NAT firewalls out there counting visitors based on IP address just doesn’t seem like a modern thing to do. I took a script by Eric Sizemore and modified it to create my own version. What it does is add to the visitor count (stored in a txt file on the server) if they don’t have a cookie showing that they have visited before, then i set that cookie to expire after 60 days, after that if the visitor comes again they will be counted again. It’s not the most accurate, but gives a good idea of how many people are visiting and i wanted to reduce server file operations.
Simple Standalone PHP Web Counter (9.6 KiB, 696 hits)
Installation & Usage
- Unzip the code into your website’s root folder
- Change permissions on the counter log txt file so that it is writable (chmod 666).
- Add the following PHP code at the top of each root page you would like to display the counter on:
<?php include('counter/counter.php'); ?>
- Add the following PHP code where you want to have the counter display:
<?php show_counter(); ?>
- Test the page you’ve included counter.php on and have show_counter() displayed.