Adding Akismet Anti-Spam Protection Anywhere

Some handy in-page links to help you navigate:

  • Add Akismet to Any Form
  • Adding Akismet to PHP Form
  • Checking for spam with Akismet
  • More about Akismet API
  • Akismet API Links

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");
[ad code=1]

Leave a comment