typecho纯代码算术验证码

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

第一步function.php如下函数

  1. //算术验证评论by minirizhi.com
  2. function themeInit($comment){
  3. $comment = spam_protection_pre($comment, $post, $result);
  4. }
  5. function spam_protection_math(){
  6. $num1=rand(1,49);
  7. $num2=rand(1,49);
  8. echo "<label for=\"math\">请输入<code>$num1</code>+<code>$num2</code>的计算结果:</label>\n";
  9. echo "<input type=\"text\" name=\"sum\" class=\"text\" value=\"\" size=\"25\" tabindex=\"4\" style=\"width:218px\" placeholder=\"计算结果:\">\n";
  10. echo "<input type=\"hidden\" name=\"num1\" value=\"$num1\">\n";
  11. echo "<input type=\"hidden\" name=\"num2\" value=\"$num2\">";
  12. }
  13. function spam_protection_pre($comment, $post, $result){
  14. $sum=$_POST['sum'];
  15. switch($sum){
  16. case $_POST['num1']+$_POST['num2']:
  17. break;
  18. case null:
  19. throw new Typecho_Widget_Exception(_t('对不起: 请输入验证码。<a href="javascript:history.back(-1)">返回上一页</a>','评论失败'));
  20. break;
  21. default:
  22. throw new Typecho_Widget_Exception(_t('对不起: 验证码错误,请<a href="javascript:history.back(-1)">返回</a>重试。','评论失败'));
  23. }
  24. return $comment;
  25. }

第二步comments.php添加函数

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

<?php spam_protection_math();?>

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

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/302
0 评论
3.6k

发表评论

!