最近经常会有垃圾评论,开了AI拦截灵敏度太高,容易误伤博友的评论,于是从网上找到了添加算术验证码的方法,并根据VOID的样式做了一下小修改。

首先打开VOID主题目录,找到functions.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行的以下代码:

/**
 * 主题启用
 */
function themeInit()
{
    Helper::options()->commentsAntiSpam = false;
    Helper::options()->commentsMaxNestingLevels = 999;
    Helper::options()->commentsOrder = 'DESC';
}

在其中添加一行代码:

$comment = spam_protection_pre($comment, $post, $result);

修改后如下:

/**
 * 主题启用
 */
function themeInit()
{
    Helper::options()->commentsAntiSpam = false;
    Helper::options()->commentsMaxNestingLevels = 999;
    Helper::options()->commentsOrder = 'DESC';
    $comment = spam_protection_pre($comment, $post, $result);
}

image-20220307200633904

接着找到VOID/includes/comments.php,在大概56行左右的位置,添加以下代码:

<div class="comment-info-input">
    <?php spam_protection_math();?>
</div>

添加完成后如下所示:

image-20220307200543434

添加完成后就可以测试一下了

image-20220307200702636

参考文章:typecho纯代码算术验证码 - 网络 - 迷你日志 (minirizhi.com)

如果觉得我的文章对你有用,请随意赞赏