代碼防SPAM

添加在在佈景函式庫functions.php即可,不過我測試的時候是無法正常提交回應,待進一步研究吧~

// 垃圾評論攔截
class anti_spam {
    function anti_spam() {
        if ( !current_user_can('level_0') ) {
            add_action('template_redirect', array($this, 'w_tb'), 1);
            add_action('init', array($this, 'gate'), 1);
            add_action('preprocess_comment', array($this, 'sink'), 1);
        }
    }
    function w_tb() {
        if ( is_singular() ) {
            ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=([\"\'])comment([\"\'])(.+)/textarea>#",
                "textarea$1name=$2w$3$4/textarea><textarea name=\"comment\" cols=\"100%\" rows=\"4\" style=\"display:none\"></textarea>",$input);') );
        }
    }
    function gate() {
        if ( !empty($_POST['w']) && empty($_POST['comment']) ) {
            $_POST['comment'] = $_POST['w'];
        } else {
            $request = $_SERVER['REQUEST_URI'];
            $referer = isset($_SERVER['HTTP_REFERER'])         ? $_SERVER['HTTP_REFERER']         : '隱瞞';
            $IP      = isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] . ' (透過D理)' : $_SERVER["REMOTE_ADDR"];
            $way     = isset($_POST['w'])                      ? '手動操作'                       : '未經評論表格';
            $spamcom = isset($_POST['comment'])                ? $_POST['comment']                : null;
            $_POST['spam_confirmed'] = "請求: ". $request. "\n來路: ". $referer. "\nIP: ". $IP. "\n方式: ". $way. "\n內容: ". $spamcom. "\n -- 記錄成功 --";
        }
    }
    function sink( $comment ) {
        if ( !empty($_POST['spam_confirmed']) ) {
            if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment;
            //方法一: 直接擋掉, 將 die(); 前面兩斜線刪除即可.
            //die();
            //方法二: 標記為 spam, 留在資料庫檢查是否誤判.
            add_filter('pre_comment_approved', create_function('', 'return "spam";'));
            $comment['comment_content'] = "[ 判斷這是 Spam! ]\n". $_POST['spam_confirmed'];
        }
        return $comment;
    }
}
$anti_spam = new anti_spam();

2015/04/04update:
仔細想想,都2015年了還搞這麼落後幹嘛?直接上大殺器badbots

  1 comment for “代碼防SPAM

  1. 小小運維 China Safari  Android 4.2.2 HTC One Build/JDQ39
    2015-04-05 at 00:00

    新年新思維!

发表评论

电子邮件地址不会被公开。 必填项已用*标注