Home > PHP > Adding Akismet.com spam checks to your contact form

Adding Akismet.com spam checks to your contact form


I had a request the other day to write a spam filter for a standard contact form. Thanks to wordpress I’ve been made aware of the excellent spam filtering service offered by Akismet.com. What’s even better, is that an Akismet Service class has been added to the Zend Framework – allowing for easy integration into one’s PHP projects.

The code posted below makes the following assumptions :

<?php
/*
* Basic function to check for spam
* @param items : associative array for containing form field values
* @return boolean : true if spam, false if clean
*/
function spamCheck($items){
 	require_once 'Zend/Service/Akismet.php'; // include the required class file - change path if necessary
 	$url = "http://url.to.my.blog.or.form"; // url associated with API key
	$api = "432dsjk890"; // Akismet API key
 	$spam = new Zend_Service_Akismet($api, $url ); // create new instance of our Akismet Service class

	if ($spam->verifyKey()){ // make sure the API key if valid before performing check
	 	$params = array(); // check the comments for the isSpam() method in Zend/Service/Askismet.php for more information on available parameters
	 	$params["user_ip"] = $_SERVER['REMOTE_ADDR']; // required by Akismet
	 	$params["user_agent"] = $_SERVER['HTTP_USER_AGENT']; // required by Akismet
	 	$params["referrer"] = $_SERVER[ 'HTTP_REFERER'];
	 	$params["comment_type"] = "comment";
	 	$params["comment_author"] = $items["name"];
	 	$params["comment_author_email"] = $items["email"];
	 	$params["comment_content"] = $items["comments"];

	 	return $spam->isSpam($params); // submits api call and returns true if spam, false if clean

	} else {
 		return false;
 	}
 }

// to make use of our spam check function try the following :
$items = sanitize($_POST); // sanitize is your own built-in function to sanitize user submitted data

// only mail the form contents if not spam
if (!spamCheck($items)){
	// insert code to mail form contents here
}
?>

This is of course a very basic example, and only touches on the isSpam() method provided by the class. The real power is actually contained in the submitSpam() and submitHam() methods on which I will be posting a tutorial shortly.

Advertisements
  1. Denham Coote
    September 13, 2008 at 2:54 pm

    I would advise that your readers look through the comments on my post to which you have linked. There have been some really good suggestions on improving the script. I will, sometime in the future, post a revised article.

    Thanks for the link! 🙂

  2. calisza
    September 13, 2008 at 4:38 pm

    Thanks Denham, I’ve added a note to the link provided, and will update the link once you’ve added the revised article.

  3. September 14, 2008 at 6:37 am

    Hi, I work with the team at Six Apart that makes TypePad AntiSpam, which is a spam prevention service that’s 100% compatible with the Akismet API, but has an open source engine and is free no matter how you use it on your site. (Akismet is not open source and places monetary restrictions on its use.)

    We’d love to see if you’d be interested in updating your example to work with TypePad AntiSpam. There are full docs at http://antispam.typepad.com/, but essentially all you need to do is change the endpoint for your code and get a free API key from the site.

  4. calisza
    September 14, 2008 at 8:31 pm

    Thanks for the info Anil. I’ve had a look at TypePad Antispam and will most definitely post another example for it shortly.

  1. September 24, 2008 at 11:34 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: