Loading... 最近经常会有垃圾评论,开了AI拦截灵敏度太高,容易误伤博友的评论,于是从网上找到了添加算术验证码的方法,并根据VOID的样式做了一下小修改。 首先打开VOID主题目录,找到`functions.php`,在末尾添加以下代码: ```php function spam_protection_math(){ $num1=rand(1,20); $num2=rand(1,20); echo "<input aria-label=\"验证码(必填)\" type=\"text\" name=\"sum\" id=\"verfiy-sum\" required placeholder=\"请计算:$num1 + $num2 = ?\" value=\"\" />"; echo "<input type=\"hidden\" name=\"num1\" value=\"$num1\">\n"; echo "<input type=\"hidden\" name=\"num2\" value=\"$num2\">"; } 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; } ``` 然后找到大概30- 40行的以下代码: ```PHP /** * 主题启用 */ function themeInit() { Helper::options()->commentsAntiSpam = false; Helper::options()->commentsMaxNestingLevels = 999; Helper::options()->commentsOrder = 'DESC'; } ``` 在其中添加一行代码: ```PHP $comment = spam_protection_pre($comment, $post, $result); ``` 修改后如下: ```PHP /** * 主题启用 */ function themeInit() { Helper::options()->commentsAntiSpam = false; Helper::options()->commentsMaxNestingLevels = 999; Helper::options()->commentsOrder = 'DESC'; $comment = spam_protection_pre($comment, $post, $result); } ```  接着找到`VOID/includes/comments.php`,在大概56行左右的位置,添加以下代码: ```PHP <div class="comment-info-input"> <?php spam_protection_math();?> </div> ``` 添加完成后如下所示:  添加完成后就可以测试一下了  参考文章:[typecho纯代码算术验证码 - 网络 - 迷你日志 (minirizhi.com)](https://minirizhi.com/22.html) © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 1 如果觉得我的文章对你有用,请随意赞赏
2 条评论
可以用在其他主题吗
只要是typecho就可以直接用,样式根据自己的主题修改一下就可以了