Jon Haworth

PHP: Random email addresses


If you leave this as it is, you'll get an 80k page with 1,000 email addresses. If you uncomment the line providing the loopback link, and a particularly stupid bot encounters your page, you'll need to be able to deal with serving constant traffic until the bot's owner stops it.

I strongly recommend including a robots exclusion meta tag in the output page - this ensures that well-behaved bots (usually belonging to reputable search engines and directories) don't get the fake addresses. Spammers and other forms of life at the bottom of the evolutionary chain write bots that deliberately ignore this standard; as ye reap, so shall ye sow, and all that.


<?php

// array of possible top-level domains
$tlds = array("com","net","gov");

// string of possible characters
$char = "0123456789abcdefghijklmnopqrstuvwxyz";

// seed random number generator - not needed if you have PHP > 4.2
mt_srand((double)microtime() * 1000000);

// uncomment the next line to provide a loopback link
#echo "<p><a href=\"". $PHP_SELF. "?". mktime(). "\">loop</a></p>\n";

// start output
echo "<p>\n";

// main loop - this gives 1,000 addresses (around 80k, usually)
for ($j = 0; $j < 1000; $j++) {

  // choose random lengths for the username ($ulen) and the domain ($dlen)
  $ulen = mt_rand(3, 8);
  $dlen = mt_rand(6, 15);

  // reset the address
  $a = "";

  // get $ulen random entries from the list of possible characters
  // these make up the username (to the left of the @)
  for ($i = 1; $i <= $ulen; $i++) 
    $a .= substr($char, mt_rand(0, strlen($char)), 1);

  // wouldn't work so well without this
  $a .= "@";

  // now get $dlen entries from the list of possible characters
  // this is the domain name (to the right of the @, excluding the tld)
  for ($i = 1; $i <= $dlen; $i++) 
    $a .= substr($char, mt_rand(0, strlen($char)), 1);

  // need a dot to separate the domain from the tld
  $a .= ".";

  // finally, pick a random top-level domain and stick it on the end
  $a .= $tlds[mt_rand(0, (sizeof($tlds) - 1))];

  // done - echo the address inside a link
  echo "\t<a href=\"mailto:". $a. "\">". $a. "</a><br />\n";

} 

// tidy up - finish the paragraph
echo "</p>\n";

?>
		

More PHP?

Photo