Are there any security concerns to consider when using the Referrer variable in PHP for redirection?

When using the Referrer variable in PHP for redirection, there is a security concern known as "Referrer spoofing," where an attacker can manipulate the Referrer header to redirect users to malicious websites. To mitigate this risk, it is recommended to validate the Referrer header before using it for redirection.

// Validate the Referrer header before using it for redirection
$referrer = $_SERVER['HTTP_REFERER'];

// Check if the Referrer header is from a trusted domain
if (strpos($referrer, 'https://example.com') !== false) {
    header('Location: ' . $referrer);
} else {
    // Redirect to a default safe URL if the Referrer header is not from a trusted domain
    header('Location: https://example.com/default');
}