aboutsummaryrefslogtreecommitdiff
path: root/counter.php
diff options
context:
space:
mode:
Diffstat (limited to 'counter.php')
-rw-r--r--counter.php27
1 files changed, 27 insertions, 0 deletions
diff --git a/counter.php b/counter.php
new file mode 100644
index 0000000..b8117e4
--- /dev/null
+++ b/counter.php
@@ -0,0 +1,27 @@
+<?php
+header('Content-Type: application/json');
+header('Cache-Control: no-store');
+
+$file = __DIR__ . '/counter.dat';
+
+// 24h cookie to dedupe reloads
+$cookieName = 'fun_seen';
+$shouldIncrement = !isset($_COOKIE[$cookieName]);
+
+if (!file_exists($file)) file_put_contents($file, '0');
+
+$fp = fopen($file, 'r+');
+if (!$fp) { echo json_encode(['count' => 0]); exit; }
+flock($fp, LOCK_EX);
+$count = (int) trim(stream_get_contents($fp));
+if ($shouldIncrement) {
+ $count++;
+ ftruncate($fp, 0);
+ rewind($fp);
+ fwrite($fp, (string) $count);
+ setcookie($cookieName, '1', time() + 86400, '/', '', true, true);
+}
+flock($fp, LOCK_UN);
+fclose($fp);
+
+echo json_encode(['count' => $count]);