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 );
Provide relevant data to akismet
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
- About Akismet
- Download Akismet
- FAQ about Akismet
- Akismet Development
- Akismet Blog
- Akismet API Documentation Version 1.1
akismet antispam anti-spam php form askapache
Related Articles
- Faster Form Submission and Processing with fsockopen
- Simple PHP Anti-Spam Captcha Script
- Faster POST and GET Form Submissions
- Auto-Login to Google Analytics to impress Clients
- Freshen your Anti-virus, Anti-Rootkits, and Anti-Spyware
- PHP Sessions/Cookies On The Fly
- Use php.ini to replace html with anything
- Debug apache log files from php
05.09.08 at 12:41 am
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!