« Smart HTTP and HTTPS .htaccess RewriteSEO Secrets of AskApache.com »
Adding Akismet Anti-Spam Protection Anywhere
May 7th, 2008
Contents
Akismet is well-known as THE anti-spam plugin for WordPress that checks every comment and trackback received by your blog for spam. Wouldn't you love to have that same kind of anti-spam protection for non-wordpress forms and pages?
Add Akismet to Any Form
This article shows you how to add this same type of anti-spam protection to any php form. It will work for any contact forms, surveys, login forms, etc.
Adding Akismet to PHP Form
First you will need to download the Micro-Akismet PHP class by Gaby Vanhegan, and add that to your server so it can be included by php like this.
include_once("class.microakismet.inc.php");
Setup Akismet Class
After you have included the class in the php file of the form you want to protect you need to activate the akismet class like so. akey is your akismet key, apage is the page the form is on, aver is your site and 1.0
$akey='4a5a26db1c'; $apage='http://www.askapache.com/about/contact/'; $aver='askapache.com/1.0'; $akismet = new MicroAkismet( $akey, $apage, $aver );
Now you need to setup an array called vars that has any information about the form submission and the user who submitted the form.
$vars = array();
foreach(array_keys($_SERVER) as $skey){
if((substr($skey, 0, 5) == "HTTP_") && !empty($_SERVER[$skey]))
$vars[str_replace('HTTP_','',$skey)]=$_SERVER[$skey];
}
$vars["user_ip"] = $_SERVER["REMOTE_ADDR"];
$vars["user_agent"] = $_SERVER["HTTP_USER_AGENT"];
$vars["comment_content"] = $_POST["Message"];
$vars["comment_author"] = $_POST["FullName"];
$vars["comment_author_email"] = $_POST["Email"];
$vars["comment_type"] = 'comment';
$vars['permalink'] = 'http://www.askapache.com'.$_SERVER['REQUEST_URI'];
$vars['referrer'] = $_SERVER['HTTP_REFERER'];
$vars['phone_number'] = $_POST['CallNumber'];
$vars['organization'] = $_POST['Organization'];
Checking for spam with Akismet
Now add this to your php. If the message is spam it will send mail with [SPAM] in the subject line, otherwise it will send mail normally.
if($akismet->check( $vars ))send_mail("[SPAM] Contact");
else send_mail("Contact");
More about Akismet API
About the Akismet Service
Akismet is basically a big machine that sucks up all the data it possibly can, looks for patterns, and learns from its mistakes. Thus far it has been highly effective at stopping spam and adapting to new techniques and attempts to evade it, and time will tell how it stands up. I've tried to keep the API interaction as simple as possible.
A Good Consumer
To interact fully with the Akismet API your program really should be putting data back into the system as well as just taking it out. If it is at all possible within the framework of your application you should have a way for your users to submit missed spam and false positives, otherwise Akismet will never learn from its mistakes.
Akismet API Links
Reader Comments
-
Супер оригинально
-
Thank you very much, i tried both akismet's provided class and yours, and yours, for some reason, is much faster.
-
Good afternoon,
First of all I would like to thanks this great tools for preventing spam message on wordpress as main tools and other website also. I have been install in my website in guestbooks page and also got an WordPress API. But when I submit my button to send comment message I got an error:
"Warning: fsockopen() [function.fsockopen]: unable to connect to rest.akismet.com:80 (Unknown error) in xxx/func.microakismet.inc.php on line 143
I dont know what wrong did I made or any missing some configuration?
Best regards
-
For some reason, I get an error on the line, if($akismet->check( $vars ))send_mail("[SPAM] Contact"); I don't think $akismet exists anywhere
-
thank you for the explanation about microakismet. i followed your directions, and instead of classetc i used func.microakismet.inc.php I am building a website off-line with WordPress and cforms for the mailforms. I am using WordPress, with cforms as a plugin for contact messages. I put the script func.microakismet.inc.php in the dir of cforms and the line include_once("func.microakismet.inc.php"); into cforms.php. All went well, I believe, till where you explain about "Provide relevant data to akismet". There you lost me. What do I need to put where in what phpfile? Could you please be a bit more specific about how to implement the script? Thanks in advance Erik Stam
-
Hey thanks alot! I've added this code to all my php forms using a simple include statement and now all my forms are stopping all the spam I used to get!
