Flexible Developments

Musings of a freelance PHP, Django and Flex Developer

Adding Akismet.com spam checks to your contact form

Posted by calisza on September 13, 2008

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.

5 Responses to “Adding Akismet.com spam checks to your contact form”

  1. Denham Coote said

    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 said

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

  3. Anil Dash said

    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 said

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

  5. [...] anti-spam, antispam, contact, form, PHP, spam, typepad, zend framework | About 2 weeks ago, I posted a quick tutorial on how to implement Akismet.com’s anti-spam checks to your contact form using the excellent [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>