Skip to content
PHP Logo

How can PHP be used to detect and block Tor requests on a website?

To detect and block Tor requests on a website using PHP, you can check the visitor's IP address against a list of known Tor exit nodes. If the IP address matches any of the Tor exit nodes, you can then block the request.

php
$tor_exit_nodes = file_get_contents('https://check.torproject.org/exit-addresses');
$tor_exit_nodes = explode("\n", $tor_exit_nodes);

$visitor_ip = $_SERVER['REMOTE_ADDR'];

foreach($tor_exit_nodes as $node) {
    if(strpos($node, 'ExitAddress') !== false) {
        $tor_ip = trim(str_replace('ExitAddress ', '', $node));
        if($visitor_ip == $tor_ip) {
            // Block the request
            header('HTTP/1.0 403 Forbidden');
            exit;
        }
    }
}