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

相关文章

php模拟socket一次连接,多次发送数据的实现代码

复制代码 代码如下: <?php //post.php function Post($host,$port) { //$host="127.0.0.1"; //建立连接 $conn...

php中文本操作的类

给大家一个简单的文本操作的类  我以前写的,不过一直都没机会用了,文本不如数据库 数据是以行保存的,以\n结尾,注意你输入的数据必须以"\n"结尾的,这是几个最基本的类成员,文...

php 无法加载mcrypt.dll的解决办法

1.php.ini里面查找extension=php_mcrypt.dll,去掉前面的分号”;” ,重启apache.无效(注意:在AppServ中php.ini在dinwos目录下)...

php处理json格式数据经典案例总结

本文实例总结了php处理json格式数据的方法。分享给大家供大家参考,具体如下: 1.json简介: 何为json? 简 单地说,JSON 可以将 JavaScript 对象中表...

php win下Socket方式发邮件类

复制代码 代码如下:<?php /* * php smtp发送邮件Scoket类 * ZhozPhpSmtpSendMail.php * Created on 2008/09/02...