Besucherzähler um Page Views erweitert
- Zwei separate Zähler: Unique Visitors (Session) + Page Views (jeden Aufruf) - Footer zeigt beide Werte an - Neue Datendateien in .gitignore aufgenommen 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,33 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* Besucherzähler API für SPA-Landing
|
||||
* Gibt den aktuellen Zählerstand als JSON zurück und zählt bei neuen Sessions
|
||||
* Zählt Unique Visitors (Session-basiert) und Page Views (jeden Aufruf)
|
||||
*/
|
||||
|
||||
header('Content-Type: application/json');
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
|
||||
$counterFile = __DIR__ . '/counter.txt';
|
||||
$visitorsFile = __DIR__ . '/visitors.txt';
|
||||
$pageviewsFile = __DIR__ . '/pageviews.txt';
|
||||
|
||||
// Zähler lesen oder initialisieren
|
||||
if (file_exists($counterFile)) {
|
||||
$count = (int) file_get_contents($counterFile);
|
||||
} else {
|
||||
$count = 0;
|
||||
}
|
||||
$visitors = file_exists($visitorsFile) ? (int) file_get_contents($visitorsFile) : 0;
|
||||
$pageviews = file_exists($pageviewsFile) ? (int) file_get_contents($pageviewsFile) : 0;
|
||||
|
||||
// Session starten für Duplikat-Erkennung
|
||||
// Page Views immer erhöhen
|
||||
$pageviews++;
|
||||
file_put_contents($pageviewsFile, $pageviews);
|
||||
|
||||
// Session starten für Unique Visitors
|
||||
session_start();
|
||||
|
||||
// Nur zählen wenn noch nicht in dieser Session gezählt
|
||||
// Unique Visitors nur bei neuer Session zählen
|
||||
if (!isset($_SESSION['spa_counted'])) {
|
||||
$count++;
|
||||
file_put_contents($counterFile, $count);
|
||||
$visitors++;
|
||||
file_put_contents($visitorsFile, $visitors);
|
||||
$_SESSION['spa_counted'] = true;
|
||||
}
|
||||
|
||||
// JSON-Antwort
|
||||
echo json_encode([
|
||||
'count' => $count,
|
||||
'formatted' => number_format($count, 0, ',', '.')
|
||||
'visitors' => $visitors,
|
||||
'visitors_formatted' => number_format($visitors, 0, ',', '.'),
|
||||
'pageviews' => $pageviews,
|
||||
'pageviews_formatted' => number_format($pageviews, 0, ',', '.')
|
||||
]);
|
||||
|
||||
Reference in New Issue
Block a user