How can the use of $_SERVER['HTTP_REFERER'] be improved or optimized to accurately track the origin of visitors in PHP?

The issue with using $_SERVER['HTTP_REFERER'] to track visitor origin is that it can be easily manipulated or spoofed by the user. To improve accuracy, you can combine it with other methods such as setting a session variable on the landing page and checking it on subsequent pages.

// Set a session variable on the landing page
session_start();
if (!isset($_SESSION['origin'])) {
    $_SESSION['origin'] = $_SERVER['HTTP_REFERER'];
}

// Check the session variable on subsequent pages
$visitor_origin = isset($_SESSION['origin']) ? $_SESSION['origin'] : 'Direct visit';
echo "Visitor origin: " . $visitor_origin;