What are some best practices for implementing page validation based on the referring URL in PHP?

Page validation based on the referring URL in PHP helps ensure that users are accessing the page from the intended source. One way to implement this is by checking the referring URL in the HTTP headers and comparing it against a list of allowed URLs. If the referring URL does not match any of the allowed URLs, you can redirect the user to a different page or display an error message.

<?php
$allowed_urls = array('https://example.com/page1', 'https://example.com/page2');

$referring_url = $_SERVER['HTTP_REFERER'];

if (!in_array($referring_url, $allowed_urls)) {
    header('Location: https://example.com/error_page.php');
    exit;
}

// Page content goes here
?>