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

yipeiwu_com5年前PHP代码库
然而,人无完人,插(件)无完插!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;
}
?>


source.zip

相关文章

centos 5.6 升级php到5.3的方法

centos 5.6 升级php到5.3的方法

升级很容易,先卸载 php 5.1.6 yum remove php* 然后安装 yum install php53* 就可以了...

Cannot modify header information错误解决方法

<?php ob_start(); setcookie("username","宋岩宾",time()+3600); echo "the username is:".$HTTP_C...

php实现查询百度google收录情况(示例代码)

写了一个小东西记录baidu和google对于站点的收录情况,现在可以查询了,其实也没什么难度,就是去file下远程文件,然后分析下。 对了貌似查google pr的东西只是file一个...

php实现解析xml并生成sql语句的方法

php实现解析xml并生成sql语句的方法

本文实例讲述了php实现解析xml并生成sql语句的方法。分享给大家供大家参考,具体如下: php解析xml有很多种办法,文档中有,搜索一下就一大把的。 今天遇到一个需求:将某个xml中...

Mac下关于PHP环境和扩展的安装详解

一直使用windows和Centos进行开发,之前公司配了Mac本,放家里吃灰了一年,新公司还是Mac,无奈只好从头摸索。 php安装主要使用brew,请注意 由于git上原homebr...