wordpress防止机器人恶意注册

代码填入主题函数文件即可

以下是代码部分:

/*注册验证*/
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('中国首都在哪里?','1+1+1等于多少?');
// 添加答案数组(与上面的问题对应)
$register_answers=array('北京','3');

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[$register_number];?><br />
<input type="text" name="user_proof" id="user_proof" class="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, $user_email, $errors) {
global $register_answers;
$register_number=$_SESSION['register_number'];
if (!isset($_POST[ 'user_proof' ]) || empty($_POST[ 'user_proof' ])) {
return $errors->add( 'proofempty', '<strong>错误</strong>: 您还没有回答问题。' );
} elseif ( strtolower( $_POST[ 'user_proof' ] ) != $register_answers[$register_number] ) {
return $errors->add( 'prooffail', '<strong>错误</strong>: 您的回答不正确。' );
}
}

「点点赞赏,手留余香」

还没有人赞赏,快来当第一个赞赏的人吧!
    3 条回复 A文章作者 M管理员
    1. […] 搬运自:https://www.logver.com/2422.html […]