PHP&JS: Liczenie wejść na stronę

Strona w trakcie budowy
// Function to send an AJAX request to update the visitor count
function updateVisitorCount() {
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "../counter.php", true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            // Optionally handle the response here
            //console.log("Visitor count updated.");
        }
    };
    xhr.send();
}
<?php
session_start();
$counter_name = "counter.txt";
 
// Check if a text file exists.
// If not, create one and initialize it to zero.
if (!file_exists($counter_name)) {
    $f = fopen($counter_name, "w");
    fwrite($f, "0");
    fclose($f);
}
 
// Read the current value of our counter file
$f = fopen($counter_name, "r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
 
// Has the visitor been counted in this session?
// If not, increase counter value by one
if (!isset($_SESSION['hasVisited'])) {
    $_SESSION['hasVisited'] = "yes";
    $counterVal++;
    $f = fopen($counter_name, "w");
    fwrite($f, $counterVal);
    fclose($f);
}
 
// Output the visitor count
header('Content-Type: text/plain');
echo "Ilość odwiedzających: " . $counterVal;
?>