PHP Classes

How to Implement a PHP Visitor Counter with a Database Using the Package Random Thing Counter App: Manage counters and count accesses with a database

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2026-02-17 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
random-thing-counter 1.0MIT/X Consortium ...7Statistics, Content management, PHP 7
Description 

Author

This package can manage counters and count accesses with a database.

It provides a router class and several controller classes that can perform operations to manage multiple site access counters.

The information about each counter and access counts is stored in a SQLite database.

Currently it can:

- Log in and log out an administrator user

- Allow an administrator user to add, delete and rename counters

- Increase a counter

- Process accesses to pages that do not exist

Picture of Ákos Nikházy
  Performance   Level  
Name: Ákos Nikházy <contact>
Classes: 18 packages by
Country: Hungary Hungary
Innovation award
Innovation award
Nominee: 8x

Instructions

Please read this document to learn and set up the visitor counter manager.

Example

<?php
/***********************
    Nikházy Ákos

index.php

Loads head with that all settings and decides what to show
to the user based on the presence and value of $_GET['view']
and the login status
***********************/

require_once __DIR__ . '/require/head.php';

$user = new User();

$view = $_GET['view'] ?? 'main';

// if user not logged in the login page is the only place they can go
if(!$user -> getLoginStatus()) $view = 'login';

$router = new Router($view);
$router->route();



Details

random-thing-counter-app

This is a very small side project, because I needed a custom counter app. You can add, delete and rename counters in it. And of course you can press the + button to count up. There is no count down. Sorry.

Warning

This was made for my personal use, on a home server to use at home without any outside world access. If you put this online please keep it in mind that this app has zero security built in. The most you can do is htaccess things as described below, but I do not recommend to use this online. I made it in 2 hours and I will not update it ever. It could be a good starting point for you to make it bigger, better, safer or submit it as a homework and get a D. This thing doesn't even handle any errors.

Install

Just copy the whole thing on your web server. There is an empty "count.db" file included too. That is yor SQLite database for this. Your webserver should have PDO SQLite installed and turned on (most have it by default)

Setup

The user name and password are admin and admin. You can generate a new one by using the following code anywhere in MainController.php's handle() method. Or in index.php or wherever you find it good to print this out.

  $pw = new Password(APPKEY); // you should change the AAPKEY value for even more safety in the require/head.php ?
  echo '<pre>';
  var_dum($pw ->createPasswordHash('YOUR PRECIOUS SECRET PASSWORD'));
  die();

Then you should edit the auth.yzhk file with the data you just printed to look like this:


That is all. 

# Secure it
You might want to ```.htaccess deny from all``` the auth file, also the count.db file too.

<Files "auth.yzhk"> Order allow,deny Deny from all </Files>

<Files "count.db">

Order allow,deny
Deny from all

</Files>


This is not at all secure. It is good enough for me to use it on my phone when I am on my home network, to count what I count.

  Files folder image Files (35)  
File Role Description
Files folder imageclasses (6 files)
Files folder imagecontroller (9 files)
Files folder imagecss (3 files, 1 directory)
Files folder imagerequire (1 file)
Files folder imagetemplate (1 file, 1 directory)
Files folder imagetext (3 files)
Accessible without login Plain text file auth.yzhk Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (35)  /  classes  
File Role Description
  Plain text file Cache.class.php Class Class source
  Plain text file Password.class.php Class Class source
  Plain text file Router.class.php Class Class source
  Plain text file Template.class.php Class Class source
  Plain text file Text.class.php Class Class source
  Plain text file User.class.php Class Class source

  Files folder image Files (35)  /  controller  
File Role Description
  Plain text file AddCountController.php Class Class source
  Plain text file AddCounterController.php Class Class source
  Plain text file BaseController.php Class Class source
  Plain text file DeleteCounterController.php Class Class source
  Plain text file FourOhFourController.php Class Class source
  Plain text file LoginController.php Class Class source
  Plain text file LogoutController.php Class Class source
  Plain text file MainController.php Class Class source
  Plain text file RenameCounterController.php Class Class source

  Files folder image Files (35)  /  css  
File Role Description
Files folder imageimg (1 file)
  Accessible without login Plain text file c.php Aux. Configuration script
  Accessible without login Plain text file index.php Aux. Configuration script
  Accessible without login Plain text file main.css Data Auxiliary data

  Files folder image Files (35)  /  css  /  img  
File Role Description
  Accessible without login Image file icon.png Icon Icon image

  Files folder image Files (35)  /  require  
File Role Description
  Accessible without login Plain text file head.php Aux. Auxiliary script

  Files folder image Files (35)  /  template  
File Role Description
Files folder imagehtml (7 files)
  Accessible without login HTML file index.html Doc. Documentation

  Files folder image Files (35)  /  template  /  html  
File Role Description
  Accessible without login HTML file error-box.html Doc. Documentation
  Accessible without login HTML file four-oh-four.html Doc. Documentation
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login HTML file login.html Doc. Documentation
  Accessible without login HTML file main-counter-line.html Doc. Documentation
  Accessible without login HTML file main.html Doc. Documentation
  Accessible without login HTML file success-box.html Doc. Documentation

  Files folder image Files (35)  /  text  
File Role Description
  Accessible without login Plain text file index.json Data Auxiliary data
  Accessible without login Plain text file login.json Data Auxiliary data
  Accessible without login Plain text file main.json Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0