Akismet误判问题与解决方案
尽管Akismet是广泛使用的工具,但它并非无懈可击。近期发现,系统常将正常的优质留言误判为垃圾信息,且若未及时干预,这些留言可能会在一段时间后自动被删除,从而造成宝贵的社区互动和友谊流失。
在使用提供的代码时,请务必记得替换其中的占位符 __YOUR_AKISMET_KEY__、__YOUR_WEBSITE_URL__ 以及 __YOUR_NAME__ 为实际有效的值。
参考来源:http://www.script-tutorials.com/akismet-spam-protection/
文件名称:index.php
具体实现代码如下:
首先引入Akismet类库文件:require_once ('classes/Akismet.class.php');
定义一个名为 MySpamProtection 的类用于处理反垃圾逻辑:class MySpamProtection {
// 成员变量声明
var $sMyAkismetKey;
var $sWebsiteUrl;
var $sAuthName;
var $sAuthEml;
var $sAuthUrl;
var $oAkismet;
// 构造函数
public function MySpamProtection() {
// 初始化必要的变量值
$this->sMyAkismetKey = '__YOUR_AKISMET_KEY__';
$this->sWebsiteUrl = '__YOUR_WEBSITE_URL__';
$this->sAuthName = '__YOUR_NAME__';
$this->sAuthEml = '';
$this->sAuthUrl = '';
// 初始化Akismet对象
$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 << Author
Comment
EOF;
if ($_POST) {
// 输出调试信息
echo '';
print_r($_POST);
echo '';
// 获取提交的信息
$sPostAuthor = $_POST['author'];
$sCommentComment = $_POST['comment'];
// 检查是否为垃圾信息
$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; }
?>source.zip