How can the fopen() function in PHP be utilized to read and compare domain names from a text file for blocking purposes?

To read and compare domain names from a text file for blocking purposes using the fopen() function in PHP, you can open the text file containing the list of domains, read each line, and compare it with the domain you want to block. If a match is found, you can take the necessary action to block the domain.

$blocklist = fopen('blocklist.txt', 'r');

$domain_to_block = 'example.com';

while(!feof($blocklist)) {
    $line = trim(fgets($blocklist));
    if($line == $domain_to_block) {
        // Block the domain
        echo "Domain $domain_to_block is blocked.";
        break;
    }
}

fclose($blocklist);