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 "\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后面的数字范围。

文章转自:https://minirizhi.com/22.html

© 版权声明
评论 抢沙发
头像
请认真填写,胡乱按键盘或文字不通不会审核通过!
提交
头像

昵称

取消
昵称表情图片