Are there alternative methods in PHP to verify and restrict access based on the referral source?

To verify and restrict access based on the referral source in PHP, you can check the HTTP referer header in the incoming request. This header contains the URL of the page that linked to the current page. By comparing this URL with a whitelist of allowed referral sources, you can control access to your PHP script.

$allowed_referrers = array('https://example.com', 'https://subdomain.example.com');

if (isset($_SERVER['HTTP_REFERER'])) {
    $referrer = parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
    
    if (!in_array($referrer, $allowed_referrers)) {
        // Redirect or deny access
        header('HTTP/1.1 403 Forbidden');
        exit();
    }
} else {
    // No referer header present, handle accordingly
}