Blackhole for Bad Bots – PHP Version

This post summarizes Blackhole for Bad Bots version 4.0+. For older versions, check out the original tutorial. Please read the original tutorial for download, demo, and important information about the standalone PHP version of Blackhole for Bad Bots. The following guide is meant to simplify things for users of Blackhole version 4.0 and better.

Trap bad bots in a virtual black hole

Bad bots are the worst. They steal server resources from legit visitors. We hates them and wants to block them. The Blackhole for Bad Bots does exactly that: blocks bad bots by trapping them in a virtual black hole. It’s a simple technique that is proven effective.

Here’s how it works: you add a hidden link to your site. You instruct bots to NOT follow that link. Any bots that disobey will be logged and denied further access. It’s that simple. To learn more, demo, and download, check out the original blackhole tutorial.

Standalone PHP Version

[ Blackhole for Bad Bots ] This article explains how to use the standalone PHP script. If you are running WordPress, the Blackhole plugin is recommended. For the PHP version, the zip download includes the /blackhole/ directory plus three files:

  • .htaccess – protects the log file
  • blackhole.dat – log file
  • index.php – blackhole script

These three files work together to create the Blackhole for Bad Bots. The .htaccess file is hidden on most systems, so if needed check out this tutorial for more infos.

Pro version! Using WordPress? Check out Blackhole Pro »


Here are the steps to install the Blackhole PHP script. Note that this applies to Blackhole version 4.0 and greater. For older versions, refer to the original blackhole tutorial. With that in mind, here are the steps:

Step 1

Download the Blackhole zip file. Unzip the file, and add the /blackhole/ folder to the root directory of your site.

Step 2

Open the index.php file, and edit the three variables in the “EDIT HERE” section.

Step 3

Change file permissions on the blackhole.dat log file, so that it’s writable by the server. Don’t worry, the .htaccess file contains rules that protect the log file.

Step 4

Include the Blackhole script by adding this line to all of your site’s pages:

<?php include(realpath(getenv('DOCUMENT_ROOT')) . '/blackhole/index.php'); ?>

That line should be placed at the top of any PHP file that displays a web page (e.g., header.php). For more information, check out the section, “File Path”, in the original blackhole tutorial.

Step 5

Add the following line to the footer of all pages (e.g., footer.php):

<a rel="nofollow" style="display:none;" href="">Do NOT follow this link or you will be banned from the site!</a>

Remember to change the href value to match the URL of your domain.

Step 6

Add these lines to your site’s robots.txt file:

User-agent: *
Disallow: /blackhole/

Note: It is important to get your robots rules correct. Please use a robots validator to verify proper syntax.

Step 7

Done! Remember to test thoroughly before going live.


You can verify that the script is working by visiting the hidden Blackhole link (added in step 5). That should take you to the Blackhole warning page, and block you from further access. To verify that you’ve been blocked, try visiting another page on your site. To restore site access, you can clear the contents of the blackhole.dat log file.

Important: Make sure that your robots rules are correct and have proper syntax. For example, you can use the robots checker in Google Webmaster Tools.

Learn more..

For complete information, please read the original Blackhole tutorial »