Online Contact Forms
I’ve had online contact forms for a while on my websites, and one thing common from site to site is that spammers will get hold of them and start trying to use them to spam me with my own tool. It’s ridiculous. I’ve tried everything to remedy the problem, from making dynamic buttons that move to different locations on the page to the use of CAPTCHA. I’ve made the forms so hard that even a person reasonably intelligent couldn’t figure it out. Everything I tried failed. Yes, even blocking ip addresses and referers because all those can be spoofed reasonably easy.
I monitored, over several years, how these guys operate. I looked at it statistically and learned that spammers are fairly predictable with phrases and word use. Using CAPTCHA, or some other type of determent will not work. Spammers have bot scripts for just about every event and can crack most determents. Heck, they have even figured out Google’s silly text CAPTCHA. Spammers are extremely good at what they do.
Because we know that spammers, for the most part, use the same keywords and terminology from message to message it becomes apparent and quite reasonable that we should be able to block them, or at least, slow them down by triggering the keywords and phrases within our code. After testing several methods, the code I devised is the simplest and has been the most effective against spammers hitting my contact forms, and guestbooks.
Form Code:
<?php
//This is the usage of the function
//Always scrub your post variables
$msg = htmlspecialchars($_POST['fmrmsg'], ENT_QUOTES);
if(msgok($msg) == "GOOD"){
//Do your form code here
echo "No bad words, you're good to go!";
}else{
//Failed the test, send them on a vacation.
header('Location: http://www.disney.com');
exit();
}
function msgok($usermsg) {
include("includes/badwords.inc");
$usermsg = strtoupper($usermsg);
$tmpbad = strtoupper($badwords);
$badwords = explode(",",$tmpbad);
$ding = "GOOD";
$i = 0;
while (($ding == "GOOD") && ($i <= count($badwords)-1)){
if (strpos($usermsg, $badwords[$i]) !== FALSE) {
$ding = "BAD";
}
$i++;
}
return $ding;
}
?>
badwords.inc Code
<?php
//add words, separated by a comma and no spaces between the commas
//If the check returns true, the email or message will not be delivered
//so be very careful what you put in here. The words are not case sensitive.
$badwords = 'enter,bad,words,or bad phrases,here';
?>
With this code I get about 1 or 2 spam messages in a month from my online forms which isn’t bad when I see about 300 of those guys using the form multiple times a month. Email me for my dictionary to enter into the $badwords variable. This may not be the best way, but it is pretty effective.