首页 » 收集 » 正文内容
给Typecho评论加上简单的拦截
寻梦xunm| 679| 收集
3年前
超过1171天 温馨提示
本文最后更新于2021年07月07日,已超过1171天没有更新,若内容或图片失效,请留言反馈。

我的评论处理逻辑是通过 ajax 提交,然后在 functions.php 中的 themeInit 函数中拦截评论请求,随后写入数据库。
作为一个懒人,我处理起来也是非常的简单哈,直接在函数中判断评论是否包含某些特定字符,如果是直接拦截掉,不是则通过。

以下就是我的具体的代码啦:

/* 主题初始化 */
function themeInit($self){
    
    /* 根据情况强制开启/关闭评论回复 */
    if($self->is('single') && $self->request->isPost() && $self->request->is('action=comment')){
        Helper::options()->commentsThreaded = true;
        if (Helper::options()->sensitiveWords) {
            if (_checkWords(Helper::options()->sensitiveWords, $self->request->text)) {
                $code = '温馨提示';
                $message = '评论包含违禁词,请友善的发布评论';
                $self->response->setStatus(200);
                require_once __TYPECHO_EXCEPTION_FILE__;
                exit;
            }
        }
    } else {
        Helper::options()->commentsThreaded = false;
    }
}
/* 判断字符串内是否包含敏感词 */
function _checkWords($words_all, $text)
{
    $words = explode("//", $words_all);
    if (empty($words)) {
        return false;
    }
    foreach ($words as $word) {
        if (false !== strpos($text, trim($word))) {
            return true;
        }
    }
    return false;
}

当然,这个只是最基础的拦截,想突破还是超级简单的。
文章来源于:https://blog.sodayang.com/post/32.html

0 赞 or 打赏
喜欢就打赏一点
微信 支付宝
隐私
Q Q:1340326824
邮箱:vipshiyi@qq.com
QQ群:422720328

我的音乐

微博客-专为自己编写开发的源码