A-A+

wordpress注册用户添加问题验证

2015年01月01日 PHP开源系统 评论 3 条 阅读 203 views 次

使用 wordpress 的用户对大量机器评论的垃圾信息深恶痛绝吧,wordpress 不仅防止垃圾评论信息是弱项,而且大量的注册垃圾用户也是头疼的事情,解决掉这种机器批量注册的情况大多用户都选择了使用验证码插件,使用验证码插件可以可以防止大多机器人注册的情况,这里再介绍一种验证问答的方法,只有知道答案的用户都可以进行注册,将下面的代码添加到主题的 functions.php 即可:

add_action( 'register_form', 'add_security_question' );
function add_security_question() { ?>
    <p>
    <label><?php _e('请输入本站域名:'xiariboke.net') ?><br />
        <input type="text" name="user_proof" id="user_proof" clas
s="input" size="25" tabindex="20" /></label>
    </p>
<?php }
      
add_action( 'register_post', 'add_security_question_validate', 10, 3 );
function add_security_question_validate( $sanitized_user_login, $u
ser_email, $errors) {
    // 如果没有回答
    if (!isset($_POST&#91; 'user_proof' &#93;) || empty($_PO
ST&#91; 'user_proof' &#93;)) {
        return $errors->add( 'proofempty', '<strong>错误</stron
g>: 您还没有回答问题。'  );
    // 如果答案不正确
    } elseif ( strtolower( $_POST[ 'user_proof' ] ) != v7v3' ) {
        return $errors->add( 'prooffail', '<strong>错误</stron
g>: 您的回答不正确。'  );
    }
}

另外还可以添加多个随机问题,代码如下:

function rand_reg_question(){
    $register_number=rand(0,1); // 设置随机数的返回范围
    $_SESSION['register_number']=$register_number;
}
add_action('login_head','rand_reg_question');
    
global $register_questions;
global $register_answers;
// 添加问题数组
$register_questions=array('中国的首都在哪里?','Googl
e是哪个国家的公司?');
// 添加答案数组(与上面的问题对应)
$register_answers=array('北京','美国');
    
add_action( 'register_form', 'add_security_question' );
function add_security_question() {
    global $register_questions;
    $register_number=$_SESSION['register_number'];
    ?>
    <p>
        <label><?php echo $register_questions&#91;$regist
er_number&#93;;?><br />
            <input type="text" name="user_proof" id="use
r_proof" class="input" size="25" tabindex="20" />
        </label>
    </p>
<?php }
    
add_action( 'register_post', 'add_security_questio
n_validate', 10, 3 );
function add_security_question_validate( $sanitized_us
er_login, $user_email, $errors) {
    global $register_answers;
    $register_number=$_SESSION&#91;'register_number'&#93;;
    if (!isset($_POST&#91; 'user_proof' &#93;) || empty($_PO
ST&#91; 'user_proof' &#93;)) {
        return $errors->add( 'proofempty', '<strong>错
误</strong>: 您还没有回答问题。' );
    } elseif ( strtolower( $_POST[ 'user_proof' ] ) != $r
egister_answers[$register_number] ) {
        return $errors->add( 'prooffail', '<strong>错
误</strong>: 您的回答不正确。' );
    }
}

问题和答案可以自己进行修改,现在刷新下注册页面看一下是否已经添加了问题验证的效果。

标签:

3 条留言  访客:3 条  博主:0 条

  1. 拽立网

    学习了!还是喜欢随机数字验证好点!

  2. 同盟源

    很好的代码

  3. 糯米汇

    验证问题好 修改成屌一点的不错

给我留言