php中使用Akismet防止垃圾评论的代码
程序员文章站
2022-05-15 20:29:07
然而,人无完人,插(件)无完插!akismet也并非完美,最近, 我常在被akismet评判为垃圾的留言中找到“好人”的留言,然而,有时时间长了就自动删除了,损失珍贵的友情...
然而,人无完人,插(件)无完插!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
别忘了修改代码中的 __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
下一篇: 花菜类蔬菜怎么做好吃