Killing Spam

4 Comments

Recently two individuals who will remain nameless (Stu Hall & Rik Lomas... ahem) were having a whale of a good time taking the piss out of my high spam count. Well laugh no longer fellas because I've come up with the answer. If you enjoy programming, particularly PHP, or even have a slight interest in the fight against spam then this may well be of interest.

The Offenders

Codename: Inconspicuous & Polite
Example: On [some date] Sue said: Thank you!
Description: This little bitch, and others like her leave seemingly innocent comments about your article, completely generic in nature. Her name of course always links to some dodgy site.
Solution: As there is nothing in the comment to identify spam we must trace it through the URL entered. A list of blocked addresses are compared and, you guessed it, URL match = no comment. I also installed an identical system to read the author name field too.
<?php
$blocked_names = array('casino','betting',etc,etc);
$blocked_urls = array('viagra.com','sex.net',etc,etc);
foreach($blocked_names as $blocked_name) {
	if(strpos(' '.strtolower($_POST['author_name']), strtolower($blocked_name)) == true) {
		$spam = true;
		break;
	}
}
foreach($blocked_urls as $blocked_url) {
	if(strpos(' '.strtolower($_POST['author_url']), strtolower($blocked_url)) == true) {
		$spam = true;
		break;
	}
}
?>				
Codename: Blunt & Painful
Example: On [some date] Blow Job said: <a href="www.sex.com">Red Hot Sex!</a>
Description: This sucka (no pun intended - honest) is perhaps the most common, though not the worst. Comments posted consist simply of a link, perhaps two, leading to a less-than-desirable location. Joy.
Solution: We already have the Name and URL bases covered (above). However now we have some new material to work with. The code below reads the comment. If it both starts and finishes with a link tag (HTML or BBcode) then the comment is thrown to the dogs.
<?php
if ((substr(strtolower($_POST['comment']),-4,4) == '</a>' || substr(strtolower($_POST['comment']),-6,6) == '[/url]') &&
(substr(strtolower($_POST['comment']),0,2) == '<a' || substr(strtolower($_POST['comment']),0,4) == '[url')) {
	$spam = true;
}
?>				
Codename: Wideboy
Example: A long, long string of URLs, way too large to fit here.
Description: These monsters are the worst. Expect at least 10 lines (depending on your line width) of pure HTML/BBcode links to every dodgy site under the sun. I don't think I need to tell you they're ugly as hell too.
Solution: There is a moderate chance that the above code will catch this big bastard for you, however to ensure none slip through the net here is some code that will count the number of <a> and [URL] tags in the comment. If more than two of each exist (this number can be changed with ease) then the comment is banned.
<?php
$url_tag_count = explode('[url',strtolower($_POST['comment']));
$a_tag_count = explode('<a',strtolower($_POST['comment']));
if (count($url_tag_count) > 2 && count($a_tag_count) > 2) {
	$spam = true;
}
?>				

I hope you've found these techniques interesting and informative. Please by all means make use of the code and pass it on if you need too. Let's hope someday we put a stop to spam once and for all!


Social Goodness

Comments

  1. On 14/06/2006 Rik Lomas said:

    Shame! Comments about tentacle porn (or whatever it was) are actually quite amusing. For me anyway...

    I found a nice link earlier which might be of some use, it redirects spambots to an error page so they never reach the site in the first place:
    <a href="http://www.homelandstupidity.us/software/bad-behavior/"> http://www.homelandstupidity.us/software/bad-behavior/</a>
    Don't know if it works, but apparently it's dead easy to set up

  2. On 14/06/2006 Pascal said:

    Hi Steve,
    Nice code. What do you use to drive you blog? Is it a homebrew code?
    I've been using Akismet (on wordpress) to catch spam comments on my site and it has managed to catch each and everyone of those annoying suckers.

    BTW: Thanks for the link on your site. I have reciprocated it...and also, I'm not an Aussie - Actually I'm a Froggy currently living in Oz!

  3. On 14/06/2006 P.J. Onori said:

    Very nice indeed. I personally use Akismet as well as personal moderation for all unique users. It has done the trick so far...

    Good luck. :)

  4. On 15/06/2006 Steve Tucker said:

    Thanks for the comments guys. @Pascal & PJ: my site is indeed 100% homegrown goodness. I was considering using an open source solution like wordpress with Akismet, but I enjoy coding far too much... half the fun of blogging!

Join the Party

*:
:
:
*:
:


Various Other Bollocks

Media Panel - Things I like

Music I Like

  • U2 - The Joshua Tree - Album Cover
  • Feeder - The Singles - Album Cover
  • The Stone Roses - Album Cover
  • Embrase - Out of Nothing - Album Cover
  • Nirvana - Incesticide - Album Cover
  • The Offspring - Smash - Album Cover

Videos I Like

  • U2 - Original of the Species - Video Screenshot
  • Nirvana - Heart Shaped Box - Video Screenshot
  • Cillet Bang Video Screenshot
  • Metallica - Unforgiven - Video Screenshot
  • U2 - Numb - Video Screenshot
  • Michael Jackson - Motown 25 - Video Screenshot

Books I Like

  • Red Dwarf, Backwards - Rob Grant - Book Cover
  • DOM Scripting - Jeremy Kieth - Book Cover
  • Foundations of Ajax - Ryan Asleson, Nate T. Schutta - Book Cover
  • The Silence of the Lambs - Thomas Harris - Book Cover
  • PHP Anthology Volume 1 - Harry Feucks - Book Cover
  • Web Standards Solutions: The Markup and Style Handbook - Dan Cederholm - Book Cover

About Steve Tucker

My name is Steve Tucker. I'm a 23 year-old web designer based in Huddersfield, England.

I design and develop for the World Wide Web and actively practice PHP, CSS, XHTML to web standards, Javascript and Ajax techniques. I also love my job, my wife, music (particularly U2), bacon sandwitches (perhaps a little too much) and a cold pint of Guinness...
Prospective stalkers click here.