Akismet – spam protection. Today we will continue PHP lessons. And today we will talk about spam protection. I think every one have own website, and every one faced with appearing of unwanted content on the site (spam). What is spam? – this is (usually) any message which not relevant to this page – usually just an advertisement of something (and even with a backward link to another site). Yes, you can put the first line of defense – a captcha, but I think spammers are also ready for this and find ways to avoid the CAPTCHA (or, they even can solve its by self). In today’s tutorial I’ll show you how to create a second line of defense against spam – using web services – for example akismet.
Akismet is good automated spam protection web service for your website(s). Firstly – open this website, sign up here and obtain own Akismet API key. After, let’s go to Plugins & libraries page, and then, select PHP 5 class by Alex. We will use this class made by Alex for our website.
Ok, here are online demo and downloadable package:
[sociallocker]
[/sociallocker]
Ok, download the example files and lets start coding !
Step 1. PHP
index.php
02 | require_once ( 'classes/Akismet.class.php' ); |
03 | class MySpamProtection { |
12 | public function MySpamProtection() { |
14 | $this ->sMyAkismetKey = '__YOUR_AKISMET_KEY__' ; |
15 | $this ->sWebsiteUrl = '__YOUR_WEBSITE_URL__' ; |
16 | $this ->sAuthName = '__YOUR_NAME__' ; |
20 | $this ->oAkismet = new Akismet( $this ->sWebsiteUrl , $this ->sMyAkismetKey); |
21 | $this ->oAkismet->setCommentAuthor( $this ->sAuthName); |
22 | $this ->oAkismet->setCommentAuthorEmail( $this ->sAuthEml); |
23 | $this ->oAkismet->setCommentAuthorURL( $this ->sAuthUrl); |
25 | public function isSpam( $s ) { |
26 | if (! $this ->oAkismet) return false; |
27 | $this ->oAkismet->setCommentContent( $s ); |
28 | return $this ->oAkismet->isCommentSpam(); |
32 | <style type= "text/css" > |
42 | <form action= "" method= "post" > |
43 | <div><label for = "author" >Author</label><input id= "author" name= "author" type= "text" value= "" /></div> |
44 | <div><label for = "comment" >Comment</label><textarea id= "comment" name= "comment" cols= "20" rows= "4" ></textarea></div> |
45 | <div><input name= "submit" type= "submit" value= "Send" /></div> |
54 | $sPostAuthor = $_POST [ 'author' ]; |
55 | $sCommentComment = $_POST [ 'comment' ]; |
57 | $oMySpamProtection = new MySpamProtection(); |
58 | $sAuthorCheck = ( $oMySpamProtection ->isSpam( $sPostAuthor )) ? ' "Author" marked as Spam' : '"Author" not marked as Spam' ; |
59 | $sCommentCheck = ( $oMySpamProtection ->isSpam( $sCommentComment )) ? ' "Comment" marked as Spam' : '"Comment" not marked as Spam' ; |
60 | echo $sAuthorCheck . '<br />' . $sCommentCheck ; |
Firstly – don`t forget to configure that class for you – apply your own __YOUR_AKISMET_KEY__, __YOUR_WEBSITE_URL__ and __YOUR_NAME__. And, you will able to use that service without problems. In this PHP code I prepared special own PHP class (MySpamProtection) which will using to check for spam. And, in second part of code – I drawing simple form, and, drawing response from Akismet plus some debug information to you.
classes/Akismet.class.php
This library I downloaded at ‘PHP 5 class by Alex’ page. Already available in our package.
Conclusion
In result, now we have pretty easy class which you can use in your own projects which will help you to validate all incoming data. So you will protected from spam. Happy coding. Good luck in your projects!