No more spam; Akismet and PHP to the rescue!
(Page 2 out of 2)Helping out Akismet
Since we're using the Akismet service in our script it's almost our honor-bound duty to help out as well, so let's do that by helping Akismet with its mistakes.
The Akismet class we're using comes with two methods to help Akismet. The first one, called submitSpam(), is used to mark a comment as spam, and it's used almost the exact same way as the isCommentSpam() method we've been using previously. See the example below:
include ('Akismet.class.php');
// Create object instance, and pass website URL and API key
$akismet = new Akismet('http://phpit.net', '[YOUR API KEY HERE]');
$comment = "Hi!\n\nI really like your website\n\n\"http://www.viagra-today.com\">Buy Viagra"
;$akismet->setCommentAuthor('Buy my viagra');
$akismet->setCommentAuthorEmail('[email protected]');
$akismet->setCommentAuthorURL('http://www.buy-viagra.com');
$akismet->setCommentContent($comment);
// Mark it as spam
$akismet->submitSpam();
?>
The second method is called submitHam(), which is used to mark a comment as not-spam (Akismet calls these good comments 'Ham'). It's used in the exact same way as the submitSpam() method:
include ('Akismet.class.php');
// Create object instance, and pass website URL and API key
$akismet = new Akismet('http://phpit.net', '[YOUR API KEY HERE]');
$comment = "Hi!\n\nI really like your website\n\n\"http://www.viagra-today.com\">Buy Viagra"
;$akismet->setCommentAuthor('Buy my viagra');
$akismet->setCommentAuthorEmail('[email protected]');
$akismet->setCommentAuthorURL('http://www.buy-viagra.com');
$akismet->setCommentContent($comment);
// Mark it as spam
$akismet->submitHam();
?>
And there you have it! You can now help the Akismet service by marking comments as spam and marking badly tagged comments as ham. Make sure you properly mark things though, and don't try to mess around with the Akismet service, because your API key could be banned.
Conclusion
In this article I've shown you how the Akismet anti-spam service works, and how to integrate it into your own PHP scripts. I've had much success with it, and so have many other people. It's probably one of the best anti-spam services around at the moment.
If you want to have a look at a few more examples of how Akismet is used, have a look at their development page and download the WordPress plugin to see a full-blown implementation of Akismet.
If you have any questions or comments about this article, please leave them below and I will respond as fast as possible. If you want to discuss this article, or something else, join us at PHPit Forums for more PHP talk.
May 24th, 2006 at 7:19 am
[…] PHPit - Totally PHP » No more spam; Akismet and PHP to the rescue! How to plug the Akismet spam filter into your own PHP website (tags: php spam akismet) […]
June 7th, 2006 at 11:59 am
Hi Dennis,
great article. Just installed Akismet (via the wp plugin) and it stopped all spam so far. Untill now I had to see that spam got through, and each time I had to copy-paste those nasty spam words to my growing list of bad words.
I will use your article to use this for a guestbook script I wrote.
However, is there any way to get me/us the class from Bret you link to (since the link doesn’t work anymore)?
(you know how to contact me)
Thanks
July 13th, 2006 at 9:12 pm
Sorry about the dead link. The one linked from the Akismet site is now active again! http://l33thaxor.com/Akismet.class.zip