From 9478c7cecd995f3cc47e0bae4768d5c558a458ff Mon Sep 17 00:00:00 2001
From: Christopher Teutsch <gh-projects@heychris.eu>
Date: Sun, 27 Dec 2020 19:08:21 +0100
Subject: [PATCH] Visitor counting with bot exclusion

---
 index.php | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)

diff --git a/index.php b/index.php
index 0bca59d..6a95112 100644
--- a/index.php
+++ b/index.php
@@ -63,8 +63,8 @@ $ical->initUrl($ICAL_URL, $acceptLanguage = 'de');
 $events = $ical->eventsFromInterval('2 week');
 $first_event = $events[0];
 $events = array_slice($events, 1);
-$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
-if ($locale !== null){
+if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])){
+	$locale = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
 	setlocale(LC_TIME, $locale);
 }
 }
@@ -178,6 +178,27 @@ $v = file_get_contents($VISITORS_FILE);
 </body>
 </html>
 <?php
-if(!$v) $v=0;
-$v++;
-file_put_contents($VISITORS_FILE, $v);
+/* Initialising values */
+if(!$v) $v=0; 
+$count_visit = true;
+/* List of bots I found in various access logs */
+$invalid_uas = array(
+	"AhrefsBot",
+	"Googlebot",
+	"Yahoo",
+	"Go-http-client/",
+	"bingbot",
+	"CheckMarkNetwork",
+	"SemrushBot",
+	"BingPreview",
+	"facebookexternalhit",
+);
+foreach ($invalid_uas as $ua){
+	if(stripos($_SERVER['HTTP_USER_AGENT'], $ua) !== FALSE){
+		$count_visit = false;
+	}
+}
+if ($count_visit === TRUE){
+	$v++;
+	file_put_contents($VISITORS_FILE, $v);
+}