eve-indy-job-tracker/track_visits.php

119 lines
No EOL
3.3 KiB
PHP

<?php
$visitsFile = "visits.json";
// Check if the cookie exists to track if the visitor has been here before
$cookieName = "EIJT";
$cookieLifetime = 60 * 60 * 24 * 30; // Cookie valid for 30 days
$firstVisit = !isset($_COOKIE[$cookieName]);
// Get user-agent for browser and OS info
$userAgent = $_SERVER["HTTP_USER_AGENT"];
$browser = "Unknown Browser";
$os = "Unknown OS";
// Detect browser using simple substr checks
if (strpos($userAgent, "Firefox") !== false) {
$browser = "Firefox";
} elseif (strpos($userAgent, "Chrome") !== false) {
$browser = "Chrome";
} elseif (strpos($userAgent, "Safari") !== false) {
$browser = "Safari";
} elseif (strpos($userAgent, "Edge") !== false) {
$browser = "Edge";
} elseif (
strpos($userAgent, "MSIE") !== false ||
strpos($userAgent, "Trident") !== false
) {
$browser = "Internet Explorer";
}
// Detect operating system
if (strpos($userAgent, "Windows NT") !== false) {
$os = "Windows";
} elseif (strpos($userAgent, "Macintosh") !== false) {
$os = "macOS";
} elseif (strpos($userAgent, "Linux") !== false) {
$os = "Linux";
} elseif (strpos($userAgent, "Android") !== false) {
$os = "Android";
} elseif (strpos($userAgent, "iPhone") !== false) {
$os = "iOS";
}
// Open the file in read/write mode, creating it if it doesn't exist
$fp = fopen($visitsFile, "c+");
if ($fp === false) {
die("Error opening the file.");
}
// Lock the file to avoid race conditions
if (flock($fp, LOCK_EX)) {
// Read the file content to get the stats
$data = fgets($fp); // Read the entire file content (assuming one line per file)
if ($data) {
$stats = json_decode($data, true);
} else {
$stats = [
"total_visits" => 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);
?>