What potential issues can arise when using the HTTP_REFERER variable in PHP?

One potential issue when using the HTTP_REFERER variable in PHP is that it can be easily spoofed by malicious users, leading to security vulnerabilities such as CSRF attacks. To mitigate this risk, it is recommended to validate and sanitize the HTTP_REFERER value before using it in your application.

// Validate and sanitize the HTTP_REFERER value
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$referer = filter_var($referer, FILTER_VALIDATE_URL);

// Check if the referer is from a trusted domain
$trusted_domain = 'https://example.com';
if (strpos($referer, $trusted_domain) !== 0) {
    // Handle unauthorized access
    die('Unauthorized access');
}

// Proceed with using the HTTP_REFERER value safely
// Your code here