php中使用Akismet防止垃圾評論的代碼
更新時(shí)間:2011年06月10日 00:49:43 作者:
Akismet是一個(gè)優(yōu)秀的防Spam垃圾留言的優(yōu)秀插件,絕大多數(shù)wordpress blogger都在使用,有了akismet之后,基本上不用擔(dān)心垃圾留言的煩惱了。
然而,人無完人,插(件)無完插!Akismet也并非完美,最近, 我常在被Akismet評判為垃圾的留言中找到“好人”的留言,然而,有時(shí)時(shí)間長了就自動(dòng)刪除了,損失珍貴的友情和留言。
別忘了修改代碼中的 __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
別忘了修改代碼中的 __YOUR_AKISMET_KEY__, __YOUR_WEBSITE_URL__ and __YOUR_NAME__
http://www.script-tutorials.com/akismet-spam-protection/
index.php
復(fù)制代碼 代碼如下:
<?
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 Ajax實(shí)現(xiàn)頁面無刷新發(fā)表評論
- PHP Ajax實(shí)現(xiàn)頁面無刷新發(fā)表評論
- 使用AngularJS和PHP的Laravel實(shí)現(xiàn)單頁評論的方法
- ThinkPHP上使用多說評論插件的方法
- 詳解WordPress中調(diào)用評論模板和循環(huán)輸出評論的PHP函數(shù)
- PHP抓取淘寶商品的用戶曬單評論+圖片+搜索商品列表實(shí)例
- PHP結(jié)合jQuery實(shí)現(xiàn)的評論頂、踩功能
- php簡單的留言板與回復(fù)功能具體實(shí)現(xiàn)
- ThinkPHP無限級(jí)分類原理實(shí)現(xiàn)留言與回復(fù)功能實(shí)例
- php實(shí)現(xiàn)評論回復(fù)刪除功能
相關(guān)文章
PHP基于ICU擴(kuò)展intl快速實(shí)現(xiàn)漢字轉(zhuǎn)拼音及按拼音首字母分組排序的方法
這篇文章主要介紹了PHP基于ICU擴(kuò)展intl快速實(shí)現(xiàn)漢字轉(zhuǎn)拼音及按拼音首字母分組排序的方法,結(jié)合實(shí)例形式分析了ICU擴(kuò)展intl的實(shí)現(xiàn)方法與拼音轉(zhuǎn)換、排序等相關(guān)操作技巧,需要的朋友可以參考下2017-05-05PHP常用設(shè)計(jì)模式之委托設(shè)計(jì)模式
委托是對一個(gè)類的功能進(jìn)行擴(kuò)展和復(fù)用的方法。它的做法是:寫一個(gè)附加的類提供附加的功能,并使用原來的類的實(shí)例提供原有的功能,接下來通過本文給大家介紹PHP委托設(shè)計(jì)模式實(shí)例詳解,感興趣的朋友一起學(xué)習(xí)吧2016-02-02如何利用php array_multisort函數(shù) 對數(shù)據(jù)庫結(jié)果進(jìn)行復(fù)雜排序
本篇文章是對用php array_multisort函數(shù)對數(shù)據(jù)庫結(jié)果進(jìn)行復(fù)雜排序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06