自从Wordpress转到Typecho以后, 一直受到垃圾评论的困扰, 加了验证码也不好使, 手动去掉email项发现垃圾评论还是会自动带上email字段.
没办法, 只好反其道而行之,
把所有评论改成必须审核, 如果正确填入了验证码, 则评论自动通过审核.
吼吼, 终于不用再为删垃圾评论费时费力了
randcode.php
<?php
//session_start();
function getAuthImage($nmsg) {
header("Content-type: image/jpeg");
$im = imagecreate(96,28);
$white = imagecolorallocate($im, 255,255,255);
$font = "arial.ttf";
// Replace path by your own font path
imagettftext($im, 16, 30-mt_rand(0,60), 10, mt_rand(20,20), imagecolorallocate($im, mt_rand(0,150),mt_rand(0,150),mt_rand(0,150)), $font, iconv("GB2312", "UTF-8", $nmsg[1]));
imagettftext($im, 16, 30-mt_rand(0,60), 30, mt_rand(20,20), imagecolorallocate($im, mt_rand(0,150),mt_rand(0,150),mt_rand(0,150)), $font, iconv("GB2312", "UTF-8", $nmsg[2]));
imagettftext($im, 16, 30-mt_rand(0,60), 50, mt_rand(20,20), imagecolorallocate($im, mt_rand(0,150),mt_rand(0,150),mt_rand(0,150)), $font, iconv("GB2312", "UTF-8", $nmsg[3]));
imagettftext($im, 16, 30-mt_rand(0,60), 70, mt_rand(20,20), imagecolorallocate($im, mt_rand(0,150),mt_rand(0,150),mt_rand(0,150)), $font, iconv("GB2312", "UTF-8", $nmsg[4]));
imagejpeg($im);
imagedestroy($im);
}
function make_rand($length="32"){//验证码文字生成函数
$str="AABCDEFGHJKLMN23456789PQRSTUVWXYZ";
$result="";
for($i=0;$i<$length;$i++){
$num[$i]=rand(1,32);
$result.="|".$str[$num[$i]];
}
return $result;
}
//创建随机码
$nmsg = make_rand(4);
//输出调用
$checkcode = make_rand(4);
session_start();//将随机数存入session中
$_SESSION["randcode"] = strtolower(implode(explode("|", $checkcode)));
getAuthImage(explode("|", $checkcode));
?>
然后在评论表单里加上验证码.
<tr>
<td><img src="/randcode.php" style="vertical-align: middle" />
<input type="text" size="4" maxlength="4" id="randcode" name="randcode"
onkeyup1="var f=document.getElementById('comment_form'),v=this.value.length;f.action=(v==4?f.title:'/');document.getElementById('form_submit').disabled=(v==4?false:'disabled');" />
<input type="submit" disabled1="disabled" value="<?php _e('提交评论'); ?>" class="submit" id="form_submit" /></td>
</tr>
再修改var\Widget\Feedback.php
$validator->addRule('text', 'required', _t('必须填写评论内容'));
$comment['text'] = $this->request->filter(array($this, 'filterText'))->text;
$randnum = strtolower($this->request->filter(array($this, 'filterText'))->randcode);
if (strtolower($this->request->filter(array($this, 'filterText'))->randcode) != $_SESSION["randcode"]) {
//$validator->addRule('randcode20060245789', 'required', _t('必须正确填写验证码'.$randnum."!=".$_SESSION["randcode"]));
$validator->addRule('randcode20060245789', 'required', _t('必须正确填写验证码'));
}
else {
$comment['status'] = "approved";
}
搞定!!!