Categories
PHP

Prevent spam in your PHP site with akismet

I’m having some problems with spam in classifieds Barcelona (works with OC), I want to keep the site as clean from spam as possible, but is not easy.

The spammers use people to submit new items, since you need to pass first a captcha and later confirm with email…but that’s not enough and I’m tired of deleting any more spam or advertisements that are not from barcelona.

What I thought is to use akismet, since in WP work pretty good.

I found this php 5 class (download) from achingbrain.net to use the akismet service with php in a really easy way.

But also what I did is to make it even easier for me, because I need to use it in many places I did this small function that only returns if is spam or not:

//Akismet spam preventing you need http://wordpress.com/api-keys/
define('AKISMET','your_api_here');//Akismet.com api key

function isSpam($name,$email,$comment){//return if something is spam or not using akismet
	if (AKISMET!=""){
		$akismet = new Akismet(SITE_URL ,AKISMET);//change this! or use defines with that name!
		$akismet->setCommentAuthor($name);
		$akismet->setCommentAuthorEmail($email);
		$akismet->setCommentContent($comment);
		return $akismet->isCommentSpam();
	}
	else return false;//we return is not spam since we don't have the api :(
}

Usage example for a contact form:

if(!isSpam($_POST["name"],$_POST["email"],$_POST["message"])){//check if is spam!
  echo "no spam! great!"
}//end akismet
else echo "Ups!Spam?";

Die SPAM!!

BTW, this will be in the next release of OC, is already implemented in all forms.