What are some alternative methods to ensure that users accessing a website from an external source are legitimate in PHP?

One alternative method to ensure that users accessing a website from an external source are legitimate in PHP is to check the HTTP referer header. This header provides information about the URL of the previous web page from which a link was followed. By verifying that the referer header matches a trusted domain or URL pattern, you can help prevent unauthorized access.

if(isset($_SERVER['HTTP_REFERER'])) {
    $referer = $_SERVER['HTTP_REFERER'];
    
    // Check if the referer matches a trusted domain or URL pattern
    if(strpos($referer, 'example.com') !== false) {
        // Proceed with the website logic
    } else {
        // Redirect or display an error message
        header('Location: unauthorized.php');
        exit;
    }
} else {
    // Handle cases where the referer header is not set
    header('Location: unauthorized.php');
    exit;
}