What is the recommended method in PHP to track the number of visitors coming from different domains to a website?

To track the number of visitors coming from different domains to a website in PHP, you can use a combination of cookies and session variables. When a visitor first lands on the website, you can set a cookie with the referring domain. Then, you can increment a session variable based on the unique referring domains. This way, you can keep track of the number of visitors coming from different domains.

// Check if the referring domain is set in the cookie
if(isset($_COOKIE['referring_domain'])) {
    $referring_domain = $_COOKIE['referring_domain'];
} else {
    $referring_domain = $_SERVER['HTTP_REFERER'];
    setcookie('referring_domain', $referring_domain, time() + 3600); // Set cookie for 1 hour
}

// Increment session variable based on unique referring domains
session_start();
if(!isset($_SESSION['visitors'][$referring_domain])) {
    $_SESSION['visitors'][$referring_domain] = 1;
} else {
    $_SESSION['visitors'][$referring_domain]++;
}

// Display the number of visitors from each domain
foreach($_SESSION['visitors'] as $domain => $count) {
    echo "Visitors from $domain: $count <br>";
}