. $dbPath = '/var/lib/sophii-counter/counter.db'; $robotsPath = '/var/www/html/robots.txt'; function isKnownBot(string $userAgent, string $robotsPath): bool { if ($userAgent === '') { return true; } if (!is_readable($robotsPath)) { return false; } static $botNames = null; if ($botNames === null) { $botNames = []; foreach (file($robotsPath, FILE_IGNORE_NEW_LINES) as $line) { if (preg_match('/^User-agent:\s*(.+)$/i', trim($line), $m)) { $name = trim($m[1]); if (preg_match('/^[A-Za-z0-9 _\-\.]+$/', $name) && strlen($name) > 2) { $botNames[] = $name; } } } } foreach ($botNames as $bot) { if (stripos($userAgent, $bot) !== false) { return true; } } $genericPatterns = '/curl|wget|python-requests|go-http-client|scrapy|headless/i'; if (preg_match($genericPatterns, $userAgent)) { return true; } return false; } $userAgent = $_SERVER['HTTP_USER_AGENT'] ?? ''; $isBot = isKnownBot($userAgent, $robotsPath); $db = new SQLite3($dbPath); $db->busyTimeout(2000); $db->exec('CREATE TABLE IF NOT EXISTS hits (id INTEGER PRIMARY KEY, count INTEGER)'); $db->exec('INSERT OR IGNORE INTO hits (id, count) VALUES (1, 0)'); if (!$isBot) { $stmt = $db->prepare('UPDATE hits SET count = count + 1 WHERE id = 1'); $stmt->execute(); } $result = $db->querySingle('SELECT count FROM hits WHERE id = 1'); header('Content-Type: text/plain'); echo $result;