0, "unique_visits" => 0, "last_visit" => null, "monthly" => [], "weekly" => [], "browsers" => [], "operating_systems" => [], ]; } // Increment the total visit count $stats["total_visits"]++; // If it's a new visitor, increase the unique visit count if ($firstVisit) { $stats["unique_visits"]++; setcookie($cookieName, "1", time() + $cookieLifetime, "/"); // Set cookie for this user } // Update the last visit timestamp $timestamp = time(); $stats["last_visit"] = $timestamp; // Track by week and month $week = date("W", $timestamp); // Week number (ISO 8601 format) $month = date("Y-m", $timestamp); // Month in YYYY-MM format // Increment weekly stats if (!isset($stats["weekly"][$week])) { $stats["weekly"][$week] = 0; } $stats["weekly"][$week]++; // Increment monthly stats if (!isset($stats["monthly"][$month])) { $stats["monthly"][$month] = 0; } $stats["monthly"][$month]++; // Track browser type if (!isset($stats["browsers"][$browser])) { $stats["browsers"][$browser] = 0; } $stats["browsers"][$browser]++; // Track operating system if (!isset($stats["operating_systems"][$os])) { $stats["operating_systems"][$os] = 0; } $stats["operating_systems"][$os]++; // Write the updated stats back to the file fseek($fp, 0); fwrite($fp, json_encode($stats)); fflush($fp); // Unlock the file flock($fp, LOCK_UN); } // Close the file fclose($fp); ?>