Typecho的垃圾评论还是比较多的,除了插件外,还可以通过PHP函数实现简单的算术验证码。

第一步function.php如下函数

//算术验证评论by minirizhi.com
function themeInit($comment){
$comment = spam_protection_pre($comment, $post, $result);
}
function spam_protection_math(){
 $num1=rand(1,49);
 $num2=rand(1,49);
 echo "\n";
 echo "\n";
 echo "\n";
 echo "";
}
function spam_protection_pre($comment, $post, $result){
 $sum=$_POST['sum'];
 switch($sum){
  case $_POST['num1']+$_POST['num2']:
  break;
  case null:
  throw new Typecho_Widget_Exception(_t('对不起: 请输入验证码。返回上一页','评论失败'));
  break;
  default:
  throw new Typecho_Widget_Exception(_t('对不起: 验证码错误,请返回重试。','评论失败'));
 }
 return $comment;
}

第二步comments.php添加函数

打开主题comments.php文件,在适当为止插入如下代码:

如果觉得100以内太难了,请修复function.php中添加的代码中rand后面的数字范围

收藏
点赞
TAGS:验证码
!如链接失效请在下方留言。本站所有资源均来源于网络,版权属于原作者!仅供学习参考,本站不对您的使用负任何责任。如果有侵权之处请第一时间联系我们删除,敬请谅解!