How to Protect any Site from Spam using Akismet

How to Protect any Site from Spam using Akismet

2 541
practice of using Akismet

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 – goto this website, sign up here and obtain own Akismet API key. After, lets goto 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:

Live Demo
download in package

Ok, download the example files and lets start coding !


Step 1. PHP

index.php

<?

require_once ('classes/Akismet.class.php');

class MySpamProtection {

    // variables
    var $sMyAkismetKey;
    var $sWebsiteUrl;
    var $sAuthName;
    var $sAuthEml;
    var $sAuthUrl;
    var $oAkismet;

    // constructor
    public function MySpamProtection() {
        // set necessary values for variables
        $this->sMyAkismetKey = '__YOUR_AKISMET_KEY__';
        $this->sWebsiteUrl = '__YOUR_WEBSITE_URL__';
        $this->sAuthName = '__YOUR_NAME__';
        $this->sAuthEml = '';
        $this->sAuthUrl = '';

        // Akismet initialization
        $this->oAkismet = new Akismet($this->sWebsiteUrl ,$this->sMyAkismetKey);
        $this->oAkismet->setCommentAuthor($this->sAuthName);
        $this->oAkismet->setCommentAuthorEmail($this->sAuthEml);
        $this->oAkismet->setCommentAuthorURL($this->sAuthUrl);
    }

    public function isSpam($s) {
        if (! $this->oAkismet) return false;

        $this->oAkismet->setCommentContent($s);
        return $this->oAkismet->isCommentSpam();
    }
}


echo <<<EOF
<style type="text/css">
form div {
    margin:10px;
}
form label {
    width:90px;
    float:left;
    display:block;
}
</style>
<form action="" method="post">
    <div><label for="author">Author</label><input id="author" name="author" type="text" value="" /></div>
    <div><label for="comment">Comment</label><textarea id="comment" name="comment" cols="20" rows="4"></textarea></div>
    <div><input name="submit" type="submit" value="Send" /></div>
</form>
EOF;

if ($_POST) {
    // draw debug information
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';

    // obtain sent info
    $sPostAuthor = $_POST['author'];
    $sCommentComment = $_POST['comment'];

    // check for spam
    $oMySpamProtection = new MySpamProtection();
    $sAuthorCheck = ($oMySpamProtection->isSpam($sPostAuthor)) ? ' "Author" marked as Spam' : '"Author" not marked as Spam';
    $sCommentCheck = ($oMySpamProtection->isSpam($sCommentComment)) ? ' "Comment" marked as Spam' : '"Comment" not marked as Spam';

    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.


Live Demo
download in 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!


2 COMMENTS

  1. I really consider this particular blog , “How
    to Protect any Site from Spam using Akismet | HTML5 and
    CSS3 Tutorials at Script Tutorials”, quite interesting plus the post ended up being a remarkable read.
    Thanks,Nichol

  2. It seems like u know quite a bit with regards to this specific subject and that shows via this amazing post,
    given the name “How to Protect any Site from Spam using Akismet | HTML5 and
    CSS3 Tutorials at Script Tutorials”. Thank you -Elbert

Leave a Reply