A simple hit counter using PDO and MySQL.
Written because, why not?
- First you'll need modify the setup script and change the following lines to your actual database connection settings.
<?php
# Database connection settings #
define('DB_NAME', 'databasename');
define('DB_USER', 'theusername');
define('DB_PASS', 'thepassword');
define('DB_HOST', 'thehostname');
then you can just execute the script by running
$ php setup.php
1.1 If you don't feel like doing all of that, you can execute the SQL below and you're set.
DROP TABLE IF EXISTS `hc_hit_count`;
CREATE TABLE `hc_hit_count` (
`route` varchar(255) NOT NULL,
`hostname` varchar(144) NOT NULL,
`hit_count` int(11) NOT NULL,
UNIQUE KEY `route_UNIQUE` (`route`,`hostname`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- Then simply include the class and initialize it
<?php
include "hitCounter.php";
$hc = new hitCounter($yourPDOconnection);
$hc->init();
?>
- That's it! it will automatically record every time a route is accessed
Since it's one file with less that 200 lines of code, there isn't much to be documented. But here's a list of the public methods:
Creates a hitCounter instance. Takes a PDO Object as a parameter and sets up the table name to be used.
Sets the current hostname and route being accessed using the php globals and then registers the hit on the database.
Registers a hit using the given route and hostname. If page was already accessed before then the hit count is increased by one instead of adding a new entry in the table.
Returns the amount of hits for the current route and hostname.
Returns the amount of hits for the given route and hostname.
The route that is being accessed
Hostname of the server, loaded from the host http header. if the header is not present then localhost is used
The PDO object passed to the constructor
Optional table name prefix used when setting up the database, default is "hc_".
The name of the table where the hit count data is going to be stored, default is "hit_count".