A fresh mix of code snippets and tutorials

Infinite loop trap for spammers

In this WP-Mix post, we’ll see how to send spammers on a virtual infinite loop chasing down randomly generated email addresses. It’s very satisfying.

When a spammer visits your site to harvest your emails, it basically records anything that “looks” like an email address. For example, “anything@anywhere.com” contains all the requisite parts, so it’s considered to be an email address. And harvesters record as many as they can get to add to their scummy mailing lists. I.e., spam.

What this PHP script does is automatically generate thousands of random email addresses whenever it is accessed. So visiting the file in your browser, you’ll see the same thing that a harvester will see: thousands of email addresses ripe for the plucking. Only these emails are bogus, leading the spammers on a wild goose chase that consumes their resources, drains their bandwidth, dilutes their potency, and ultimately costs them time and precious money. Punishment fits the crime is an understatement.

Infinite loop trap

Create a blank PHP file named “emails.php” or whatever, and upload to the directory of your choice. Inside the file, include the following script:

<?php // infinite loop trap for spammers
$limit = 5000; // specify the number of emails
$page = '';
for ($i = 0; $i < $limit; $i++) {
	$page .= generate_emails(); 
function generate_emails() { 
	$email = '';
	$chars = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','-'); 
	for ( $i = 0; $i < 17; $i++ ) {
		$email .= ( $i !== 10 ) ? $chars[ mt_rand( 0, 25 ) ] : '@';
	$email .= '.com'; 
	$email  = '<a href="mailto:' . $email . '">' . $email . "</a>\n"; 
	return $email; 
$page .= "Have fun with that :)"; 
echo $page; ?>

Things to edit: the number of emails to generate. Other aspects may be fine-tuned as well. Optionally you can include a line in your site’s robots.txt file that explicitly forbids access of your email trap:

User-agent: *
Disallow: /emails.php

Now get out there and feed those spammers :)

★ Pro Tip: