PHP Classes

Simple PHP Visitor Counter: Count and display the visits that site pages had

Recommend this page to a friend!
  Info   View files Documentation   View files View files (47)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-11 (3 months ago) RSS 2.0 feedNot enough user ratingsTotal: 178 All time: 8,734 This week: 99Up
Version License PHP version Categories
simple_counter 5.0.0GNU Lesser Genera...8.2.0Statistics, User Management, PHP 7
Description 

Author

This class can count and display the visits that site pages had.

It uses local files to track the count of unique users that visit a given page.

A user is determined to be unique based on the device's IP address that is used to access the page. Private IP addresses are ignored.

The package uses file locks to prevent simultaneous accesses from corrupting the file on which the user counting is stored.

It can also display the current page user visits count using an image or a text message.

Picture of Eric Sizemore
  Performance   Level  
Name: Eric Sizemore <contact>
Classes: 15 packages by
Country: United States United States
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Documentation

Simple Counter - A simple web hit counter.

FOSSA Status Build Status Code Coverage Scrutinizer Code Quality Tests PHPStan

Latest Stable Version Downloads per Month License

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)

Small Note

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. ;)

If you are upgrading from v4.* to v5.0.0

  • Make a backup of your current `logs` directory (and `images`, if you are using custom images). * Your directories are most likely `counter/logs` and `counter/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.
  • Install SimpleCounter v5 through composer.
  • Copy your `ips.txt` and `counter.txt` files that you backed up to the new locations.
  • Update your site/project on how you call the counter, see usage.

[^1]: If you were using custom locations for the logs and images directories already, you can change how you instantiate the class to point to these locations instead. See usage.

Installation

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 from `vendor/esi/simple_counter` to your webroot * The `counter` directory contains the `logs` and `images` directory. * You can change the name of either directory if you wish, or skip using the `counter` directory all together and just move `logs` and `images` to your webroot. * However: * The ip file and counter file must remain `ips.txt` and `counter.txt` * The images must be named 0-9.
  • Make sure the `ips.txt` and `counter.txt` files within your logs directory are writable.

Usage

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;

?>

About

Requirements

  • Simple Counter works with PHP 8.2.0 or above.

Submitting bugs and feature requests

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)

Contributing

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.

Guidelines

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.

PHP Style

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.

Documentation

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.

Documenting functions/variables (PHPDoc)

Please ensure all new contributions adhere to: * PSR-5 PHPDoc * PSR-19 PHPDoc Tags

when documenting new functions, or changing existing documentation.

Branching

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.

Author

Eric Sizemore - <admin@secondversion.com> - <https://www.secondversion.com>

License

Simple Counter is licensed under the GNU LGPL v3 License - see the COPYING file for details


  Files folder image Files  
File Role Description
Files folder image.github (2 files, 1 directory)
Files folder imagecounter (2 directories)
Files folder imagesrc (1 file)
Files folder imagetests (1 file, 3 directories)
Accessible without login Plain text file .php-cs-fixer.dist.php Aux. Auxiliary script
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file COPYING Data Auxiliary data
Accessible without login Plain text file COPYING.LESSER Data Auxiliary data
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file rector.php Aux. Auxiliary script
Accessible without login Plain text file SECURITY.md Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (2 files)
  Accessible without login Plain text file dependabot.yml Data Auxiliary data
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file main.yml Data Auxiliary data
  Accessible without login Plain text file tests.yml Data Auxiliary data

  Files folder image Files  /  counter  
File Role Description
Files folder imageimages (11 files)
Files folder imagelogs (3 files)

  Files folder image Files  /  counter  /  images  
File Role Description
  Accessible without login Image file 0.gif Icon Icon image
  Accessible without login Image file 1.gif Icon Icon image
  Accessible without login Image file 2.gif Icon Icon image
  Accessible without login Image file 3.gif Icon Icon image
  Accessible without login Image file 4.gif Icon Icon image
  Accessible without login Image file 5.gif Icon Icon image
  Accessible without login Image file 6.gif Icon Icon image
  Accessible without login Image file 7.gif Icon Icon image
  Accessible without login Image file 8.gif Icon Icon image
  Accessible without login Image file 9.gif Icon Icon image
  Accessible without login Image file spacer.gif Icon Icon image

  Files folder image Files  /  counter  /  logs  
File Role Description
  Accessible without login Plain text file .htaccess Data Auxiliary data
  Accessible without login Plain text file counter.txt Data Auxiliary data
  Accessible without login Plain text file ips.txt Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Plain text file Counter.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageimages (11 files)
Files folder imagelogs (3 files)
Files folder imagesrc (1 file)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  images  
File Role Description
  Accessible without login Image file 0.gif Icon Icon image
  Accessible without login Image file 1.gif Icon Icon image
  Accessible without login Image file 2.gif Icon Icon image
  Accessible without login Image file 3.gif Icon Icon image
  Accessible without login Image file 4.gif Icon Icon image
  Accessible without login Image file 5.gif Icon Icon image
  Accessible without login Image file 6.gif Icon Icon image
  Accessible without login Image file 7.gif Icon Icon image
  Accessible without login Image file 8.gif Icon Icon image
  Accessible without login Image file 9.gif Icon Icon image
  Accessible without login Image file spacer.gif Icon Icon image

  Files folder image Files  /  tests  /  logs  
File Role Description
  Accessible without login Plain text file .htaccess Data Auxiliary data
  Accessible without login Plain text file counter.txt Data Auxiliary data
  Accessible without login Plain text file ips.txt Data Auxiliary data

  Files folder image Files  /  tests  /  src  
File Role Description
  Plain text file CounterTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:178
This week:0
All time:8,734
This week:99Up