How can PHP code be structured to effectively block specific referrers and display custom ErrorDocuments in a PHP website?

To block specific referrers and display custom ErrorDocuments in a PHP website, you can use the htaccess file to set up rules for blocking referrers and redirecting to custom error pages. Additionally, you can use PHP code to handle the redirection and display the custom ErrorDocuments based on the referrer.

<?php
$blocked_referrers = array("blockedreferrer1.com", "blockedreferrer2.com");

if (in_array($_SERVER['HTTP_REFERER'], $blocked_referrers)) {
    header("Location: /error_page.php");
    exit;
}
?>