php中使用Akismet防止垃圾評(píng)論的代碼
更新時(shí)間:2011年06月10日 00:49:43 作者:
Akismet是一個(gè)優(yōu)秀的防Spam垃圾留言的優(yōu)秀插件,絕大多數(shù)wordpress blogger都在使用,有了akismet之后,基本上不用擔(dān)心垃圾留言的煩惱了。
然而,人無(wú)完人,插(件)無(wú)完插!Akismet也并非完美,最近, 我常在被Akismet評(píng)判為垃圾的留言中找到“好人”的留言,然而,有時(shí)時(shí)間長(zhǎng)了就自動(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)頁(yè)面無(wú)刷新發(fā)表評(píng)論
- PHP Ajax實(shí)現(xiàn)頁(yè)面無(wú)刷新發(fā)表評(píng)論
- 使用AngularJS和PHP的Laravel實(shí)現(xiàn)單頁(yè)評(píng)論的方法
- ThinkPHP上使用多說(shuō)評(píng)論插件的方法
- 詳解WordPress中調(diào)用評(píng)論模板和循環(huán)輸出評(píng)論的PHP函數(shù)
- PHP抓取淘寶商品的用戶曬單評(píng)論+圖片+搜索商品列表實(shí)例
- PHP結(jié)合jQuery實(shí)現(xiàn)的評(píng)論頂、踩功能
- php簡(jiǎn)單的留言板與回復(fù)功能具體實(shí)現(xiàn)
- ThinkPHP無(wú)限級(jí)分類原理實(shí)現(xiàn)留言與回復(fù)功能實(shí)例
- php實(shí)現(xiàn)評(píng)論回復(fù)刪除功能
相關(guān)文章
php從memcache讀取數(shù)據(jù)再批量寫(xiě)入mysql的方法
這篇文章主要介紹了php從memcache讀取數(shù)據(jù)再批量寫(xiě)入mysql的方法,可利用memcache緩解服務(wù)器讀寫(xiě)壓力,并實(shí)現(xiàn)數(shù)據(jù)庫(kù)數(shù)據(jù)的寫(xiě)入操作,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
php通過(guò)sort()函數(shù)給數(shù)組排序的方法
這篇文章主要介紹了php通過(guò)sort()函數(shù)給數(shù)組排序的方法,實(shí)例分析了php中sort()函數(shù)的功能及相關(guān)使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
PHP創(chuàng)建簡(jiǎn)單RPC服務(wù)案例詳解
這篇文章主要介紹了PHP創(chuàng)建簡(jiǎn)單RPC服務(wù)案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
PHP數(shù)據(jù)庫(kù)操作之基于Mysqli的數(shù)據(jù)庫(kù)操作類庫(kù)
Mysqli 是什么,我這里也不進(jìn)行描述了。因?yàn)榫W(wǎng)上關(guān)于 Mysqli 的教程數(shù)不勝數(shù),我這里為大家介紹一款基于 Mysqli 的操作數(shù)據(jù)庫(kù)類(M.class.php)2014-04-04
在MongoDB中模擬Auto Increment的php代碼
MySQL用戶多半都有Auto Increment情結(jié),不過(guò)MongoDB缺省并沒(méi)有實(shí)現(xiàn),所以需要模擬一下,編程語(yǔ)言以PHP為例2011-03-03
php 截取GBK文檔某個(gè)位置開(kāi)始的n個(gè)字符方法
下面小編就為大家?guī)?lái)一篇php 截取GBK文檔某個(gè)位置開(kāi)始的n個(gè)字符方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
php通過(guò)pecl方式安裝擴(kuò)展的實(shí)例講解
下面小編就為大家分享一篇php通過(guò)pecl方式安裝擴(kuò)展的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
PHP實(shí)現(xiàn)的文件瀏覽器功能簡(jiǎn)單示例
這篇文章主要介紹了PHP實(shí)現(xiàn)的文件瀏覽器功能,結(jié)合完整實(shí)例形式分析了php針對(duì)目錄與文件的遍歷、判斷、屬性讀取等相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
Laravel框架數(shù)據(jù)庫(kù)遷移操作實(shí)例詳解
這篇文章主要介紹了Laravel框架數(shù)據(jù)庫(kù)遷移操作實(shí)例詳解,需要的朋友可以參考下2020-04-04

