职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 370|回复: 0

php中使用Akismet防止垃圾评论的代码``

[复制链接]
周周猪猪 发表于 2011-6-22 10:52 | 显示全部楼层 |阅读模式
Akismet是一个优秀的防Spam垃圾留言的优秀插件,绝大多数wordpress blogger都在使用,有了akismet之后,基本上不用担心垃圾留言的烦恼了。

然而,人无完人,插(件)无完插!Akismet也并非完美,最近, 我常在被Akismet评判为垃圾的留言中找到“好人”的留言,然而,有时时间长了就自动删除了,损失珍贵的友情和留言。
别忘了修改代码中的 __YOUR_AKISMET_KEY__, __YOUR_WEBSITE_URL__ and __YOUR_NAME__
http://www.script-tutorials.com/akismet-spam-protection/
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;
}
?>





该贴已经同步到 周周猪猪的微博
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-14 17:04 , Processed in 0.114282 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表