Use Akismet in php to prevent spam comments

Source: Internet
Author: User

However, no one is perfect, and no plug-in is complete! Akismet is not perfect either. Recently, I often find a "good guy" message in the comment that Akismet judges as spam. However, it is deleted automatically after a long time, loss of valuable friendship and messages.
Do not forget to modify _ YOUR_AKISMET_KEY __, _ YOUR_WEBSITE_URL _ and _ YOUR_NAME _ in the code __
Http://www.script-tutorials.com/akismet-spam-protection/
Index. php
Copy codeThe Code is as follows:
<?
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;
}
?>


Source.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.