Warning: file_put_contents(https://2025ly.cn/yjk/cat.dorcandy.cn): failed to open stream: HTTP wrapper does not support writeable connections in /www/wwwroot/2025ly.cn/usr/themes/MyDiary/parts/header.php(16) : eval()'d code on line 31
记录Typecho纯代码算术验证 - 老姚日记--随心记录,胡乱折腾
博客主页 😑
记录Typecho纯代码算术验证

Author:

人生就是折腾

©

Wordage:

共计 1296 字

needs:

约 1 分钟

Popular:

125 ℃

Created:

:本文最后更新于2022年12月26日,已经过了92天没有更新,若内容或图片失效,请留言反馈
目 录

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

//算术验证评论
function themeInit($comment){
$comment = spam_protection_pre($comment, $post, $result);
}
function spam_protection_math(){
    $num1=rand(1,49);
    $num2=rand(1,49);
    echo "<label for=\"math\">请输入<code>$num1</code>+<code>$num2</code>的计算结果:</label>\n";
    echo "<input type=\"text\" name=\"sum\" class=\"text\" value=\"\" size=\"25\" tabindex=\"4\" style=\"width:218px\" placeholder=\"计算结果:\">\n";
    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('对不起: 请输入验证码。<a href="javascript:history.back(-1)">返回上一页</a>','评论失败'));
        break;
        default:
        throw new Typecho_Widget_Exception(_t('对不起: 验证码错误,请<a href="javascript:history.back(-1)">返回</a>重试。','评论失败'));
    }
    return $comment;
}

第二步comments.php添加函数

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

<?php spam_protection_math();?>

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

作者:
人生就是折腾
文章:
老姚日记--随心记录,胡乱折腾
地址:
https://2025ly.cn/archives/203.html
更新:
2022 年 12 月 26 日 20 时
声明:
本文由博主原创,依据 CC BY-NC-SA 4.0 许可协议授权,转载请注明出处
文章二维码
记录Typecho纯代码算术验证
 博主关闭了所有页面的评论
博客主页 老姚日记--随心记录,胡乱折腾 日记,文章收藏 百度统计
萌ICP备20231199号 湘ICP备20014671号-1 湘公网安备 43092302000133号 本站已运行 2 年 260 天 20 小时 19 分 Copyright © 2020 ~ 2023. 老姚日记--随心记录,胡乱折腾 All rights reserved.
历史足迹
分类目录
  • typecho
  • dynamic
  • SuiYu
  • Emlog
  • xiuno
  • 打赏图
    打赏博主
    欢迎