Portable Spam Prevention

A blacklist can be a handy tool for preventing a certain kind of spam; the kind that seeks to gain page rank by having lots of links back to a certain page. It works because the URL is something the spammer must include if they want their spam to have any effect. If you build a blacklist in a plain text file, with one keyword (could be a domain, a word, whatever) for each line then it is really easy to plug into that list from a variety of applications.

I was getting some post spam in my PunBB forums, so I hooked up the forum posting to the same blacklist I use to prevent comment spam here in my blog. Here is the PHP code if you’d like to try it yourself:

$spam_urls = file("spam_urls.txt");
foreach ($spam_urls as $spam_url) {
$spam_url = str_replace("\n", "", strtolower($spam_url));
if (strstr(strtolower(stripslashes($_REQUEST["field_name"])), $spam_url)) {
die('Oops, blocked.');

To use:

  1. build your spam_urls.txt file
  2. change the variable (field_name) to the name of the field that can contain spam URLs (you can add additional fields as needed, the easiest way is to copy the “if” block)
  3. add this check that into the code just before something gets posted

If you use WordPress and like managing your blacklist in WordPress, you can set up a CRON job to create the text file from the blacklist in the database once an hour or so. If you only have the list in the database, it’s harder to leverage the list from other applications.