Are there any best practices for implementing a Referrer redirect in PHP?
When implementing a Referrer redirect in PHP, it is important to validate the referrer URL to prevent malicious redirects. One best practice is to check if the referrer URL is from a trusted domain before performing the redirect. This can help prevent security risks such as open redirect vulnerabilities.
<?php
$allowed_domains = array("example.com", "trusteddomain.com");
$referrer = $_SERVER['HTTP_REFERER'];
if ($referrer && in_array(parse_url($referrer, PHP_URL_HOST), $allowed_domains)) {
header("Location: https://example.com/redirected-page.php");
exit();
} else {
echo "Referrer not allowed";
}
?>