Analytics & Utilities

Privacy-Friendly PHP Visitor Counter

Daily aggregate page counter without storing raw IP addresses or user-agent strings.

Overview

Daily aggregate page counter without storing raw IP addresses or user-agent strings.

What this script does

This starter demonstrates the core implementation pattern. Adapt database fields, configuration and error handling to your application.

Source code

<?php
declare(strict_types=1);
function countPageView(PDO $pdo,string $pageKey): void {
    $pageKey=substr($pageKey,0,190);
    $s=$pdo->prepare('INSERT INTO daily_page_views(page_key,view_date,views) VALUES(:key,CURRENT_DATE(),1) ON DUPLICATE KEY UPDATE views=views+1');
    $s->execute(['key'=>$pageKey]);
}

Download starter source: Download Privacy-Friendly PHP Visitor Counter source

Installation and customization

  1. Review the code and required PHP extensions.
  2. Configure database and storage settings outside the public web root where applicable.
  3. Add authorization, rate limiting and logging for public-facing endpoints.
  4. Test valid, invalid and boundary inputs before deployment.

> This is a learning starter, not a complete production application. Apply the security and deployment requirements of your project.