A few sites I administer have recently had the misfortune of having spambots visit their enquiry and contact pages. These pages usually have a contact form, where an enquirer can leave their name, e-mail, and request or comment. When they submit the form, a copy of the message is e-mailed to the site owner. The spambots try and submit messages that usually contain gibberish but also multiple URLs to spam sites. Something had to be done to prevent site owners from receiving hundreds of spam messages a day.
I considered a few methods for preventing the bots from visiting the enquiry page. These included firewall configuration, user-agent detection, rudimentary parsing of the messages, captcha systems, and so forth. These methods were either too cumbersome to implement, could be circumvented, or spoiled the user experience for a genuine user. The latter was a critical concern.
Enter Akismet
The Akismet API is an open API used to assess the spam score of comments left or enquiries made on a site. It is in widespread use as a plugin for WordPress blogs. Its effectiveness has become a must-have plugin for WordPress installations. The Akismet API however can be applied to any site or application capable of making HTTP requests.
First you need an API key. You can obtain one by registering for a WordPress.com user account (you do not need to have an active WordPress blog). Your API key will be e-mailed to you once you have activated your account.
Akismet and PHP5
Download the PHP5 Akismet Library.
Extract the contents of the downloaded package and place them in a location that your application can access when required.
Here's how to use the Akismet API in your PHP5 code.
-
require_once('Akismet.class.php');
-
-
$API_key = 'xxxxxxxxxxxx';
-
$source_url = 'http://www.mysite.com/contact.php';
-
-
$akismet = new Akismet($source_url, $API_key);
-
$akismet->setCommentAuthor($enquirer_name);
-
$akismet->setCommentAuthorEmail($enquirer_email);
-
$akismet->setCommentContent($enquiry);
-
-
if ($akismet->isCommentSpam()){
-
//Enquiry is spammy - log it for later review by site owner
-
//If false positive, be sure to submit to Akismet so that it can learn from
-
// its mistake. Use Akismet::submitHam()
-
} else {
-
//Enquiry is not spammy - e-mail it to the site owner
-
//If false, be sure to submit to Akismet so that it can train itself better.
-
// Use Akismet::submitSpam()
-
}
-
-
//Below are other Akismet methods that you could call
-
$akismet->setCommentAuthorURL($enquirer_url);
-
$akismet->setCommentType($enquiry_type); //{'blank', 'comment', 'trackback', 'pingback', or custom}
-
$akismet->setPermalink($url); //A permanent URL referencing the resource for which a comment is being left for
Akismet and PHP4
Download the PHP4 Akismet library. Extract the contents of the downloaded package and place them in a location that your application can access when required.
Here's how to use the Akismet API in your PHP4 code.
-
require_once('Akismet.class.php');
-
-
$API_key = 'xxxxxxxxxxxx';
-
$source_url = 'http://www.mysite.com/contact.php';
-
-
'email' => $enquirer_email,
-
'website' => $enquirer_uri,
-
'body' => $enquiry,
-
'permalink' => $this_page_uri,
-
'user_ip' => $referrer_ip, // optional, defaults to $_SERVER['REMOTE_ADDR']
-
'user_agent' => $client_ua, // optional, defaults to $_SERVER['HTTP_USER_AGENT']
-
);
-
-
$akismet = new Akismet($source_url, $API_key, $comment);
-
-
// test for errors before submitting to Akismet
-
if($akismet->errorsExist()) {
-
if($akismet->isError('AKISMET_INVALID_KEY')) {
-
//...
-
} elseif($akismet->isError('AKISMET_RESPONSE_FAILED')) {
-
//...
-
} elseif($akismet->isError('AKISMET_SERVER_NOT_FOUND')) {
-
//...
-
}
-
} elseif ($akismet->isSpam()) {
-
//Enquiry is spammy - log it for later review by site owner
-
//If false positive, be sure to submit to Akismet so that it can learn from
-
// its mistake. Use Akismet::submitHam()
-
} else {
-
//Enquiry is not spammy - e-mail it to the site owner
-
//If false, be sure to submit to Akismet so that it can train itself better.
-
// Use Akismet::submitSpam()
-
}
