Simple Counter is a simple PHP counter that counts your website visitors. It has the ability to either show the count as plain text or images; and whether to count only unique hits, or all hits. (IP Based)
This code is many years old (first started in 2006). I am in the process of bringing it into the modern world, and that includes the documentation. I am working on it, I promise. ;)
- Make a backup of your current
logs
directory (andimages
, if you are using custom images).- Your directories are most likely
counter/logs
andcounter/images
since that was the default way to install/setup SimpleCounter prior to v5 1 - This is the default in v5 as well, so if you follow installation and copy the
counter
folder to your webroot, it will overwrite your old data, so backups are key.
- Your directories are most likely
- Install SimpleCounter v5 through composer.
- Copy your
ips.txt
andcounter.txt
files that you backed up to the new locations. - Update your site/project on how you call the counter, see usage.
To install Simple Counter, first install via composer:
composer require esi/simple_counter
There are several options defined by default, however you will likely run into issues if you do not change some of them. More information can be found in Usage below.
- Copy the
counter
directory fromvendor/esi/simple_counter
to your webroot- The
counter
directory contains thelogs
andimages
directory. - You can change the name of either directory if you wish, or skip using the
counter
directory all together and just movelogs
andimages
to your webroot.- However:
- The ip file and counter file must remain
ips.txt
andcounter.txt
- The images must be named 0-9.
- The ip file and counter file must remain
- However:
- The
- Make sure the
ips.txt
andcounter.txt
files within your logs directory are writable.
Usage is fairly simple once installed. Simply add the following code to the page where you want the counter to be shown:
<?php
// Load the composer autoload file, if not already loaded
require_once 'vendor/autoload.php';
use Esi\SimpleCounter\Counter;
// Options is a string => string | bool, key => value pair array.
// Valid options are:
$options = [
'logDir' => '/path/to/some/dir/logs',
'imageDir' => '/path/to/some/dir/images',
'imageExt' => '.gif', // '.png', '.jpg' etc. default images are GIF images
'useImages' => true, // true = images, false = plain text
'useFileLocking' => true, // recommended = true for file locking on read/write operations for the log files
'countOnlyUnique' => true // true = counts only unique ip's, false = counts all
];
// If no options are passed, uses defaults
$counter = Counter::getInstance();
// ... or maybe custom directories
$counter = Counter::getInstance([
'logDir' => '/var/www/html/hitcounter/logs',
'imageDir' => '/var/www/html/hitcounter/images'
]);
// ... or maybe defaults, but I want to use custom images and higher quality PNG images
$counter = Counter::getInstance([
'imageExt' => '.png'
]);
// Finally, call process(). You can either output it directly or save it to a variable if needed
echo $counter->process();
// ... or ...
$hitCount = $counter->process();
// ..
// ...
// ... do some stuff
echo $hitCount;
?>
- Simple Counter works with PHP 8.2.0 or above.
Bugs and feature requests are tracked on GitHub
Issues are the quickest way to report a bug. If you find a bug or documentation error, please check the following first:
- That there is not an Issue already open concerning the bug
- That the issue has not already been addressed (within closed Issues, for example)
Simple Counter accepts contributions of code and documentation from the community. These contributions can be made in the form of Issues or Pull Requests on the Simple Counter repository.
Simple Counter is licensed under the GNU LGPL v3 license. When submitting new features or patches to Simple Counter, you are giving permission to license those features or patches under the GNU LGPL v3 license.
Simple Counter tries to adhere to PHPStan level 9 with strict rules and bleeding edge. Please ensure any contributions do as well.
Before we look into how, here are the guidelines. If your Pull Requests fail to pass these guidelines it will be declined, and you will need to re-submit when you’ve made the changes. This might sound a bit tough, but it is required for me to maintain quality of the code-base.
Please ensure all new contributions match the PSR-12 coding style guide. The project is not fully PSR-12 compatible, yet; however, to ensure the easiest transition to the coding guidelines, I would like to go ahead and request that any contributions follow them.
If you change anything that requires a change to documentation then you will need to add it. New methods, parameters, changing default values, adding constants, etc. are all things that will require a change to documentation. The change-log must also be updated for every change. Also, PHPDoc blocks must be maintained.
Please ensure all new contributions adhere to:
when documenting new functions, or changing existing documentation.
One thing at a time: A pull request should only contain one change. That does not mean only one commit, but one change - however many commits it took. The reason for this is that if you change X and Y but send a pull request for both at the same time, we might really want X but disagree with Y, meaning we cannot merge the request. Using the Git-Flow branching model you can create new branches for both of these features and send two requests.
Eric Sizemore - admin@secondversion.com - https://www.secondversion.com
Simple Counter is licensed under the GNU LGPL v3 License - see the COPYING
file for details