FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home » PHP » Adding Akismet Anti-Spam Protection Anywhere

by 6 comments

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

Tags

Comments Welcome

Information is freedom. Freedom is non-negotiable. So please feel free to modify, copy, republish, sell, or use anything on this site in any way at any time ;)

My Online Tools

Popular Articles
Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two.
-- Richard M. Stallman


It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | htaccess.io | htaccess.guru

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service | @Htaccess

↑ TOPMain