Home > php教程 > PHP源码 > 使用 Akismet 防止垃圾评论

使用 Akismet 防止垃圾评论

PHP中文网
Release: 2016-05-25 17:14:00
Original
1146 people have browsed it

Akismet是一个优秀的防Spam垃圾留言的优秀插件,绝大多数wordpress blogger都在使用,有了akismet之后,基本上不用担心垃圾留言的烦恼了。然而,人无完人,插(件)无完插!Akismet也并非完美,最近, 我常在被Akismet评判为垃圾的留言中找到“好人”的留言,然而,有时时间长了就自动删除了,损失珍贵的友情和留言。

别忘了修改代码中的 __YOUR_AKISMET_KEY__, __YOUR_WEBSITE_URL__ and __YOUR_NAME__

<?

require_once (&#39;classes/Akismet.class.php&#39;);

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 = &#39;__YOUR_AKISMET_KEY__&#39;;
 $this->sWebsiteUrl = &#39;__YOUR_WEBSITE_URL__&#39;;
 $this->sAuthName = &#39;__YOUR_NAME__&#39;;
 $this->sAuthEml = &#39;&#39;;
 $this->sAuthUrl = &#39;&#39;;

 // 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 &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r($_POST);
 echo &#39;
'; // 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 . '
' . $sCommentCheck; } ?>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template