WordPress 注册表单添加验证问题(支持多个随机问题)

愿我们努力拼搏,征战沙场,不忘初心,努力成为一个浑身充满铜臭味的有钱人。

通常网站的注册表单都使用验证码来进行验证,但是有没有考虑过使用验证问题来验证呢?使用问题验证的好处在于:防止机器人注册(和验证码一样),只有知道答案的人才能注册(可用于限制用户注册)。下面将添加一个验证问题:中国的首都是哪里?答案是个正常人都知道:北京。

将下面的代码添加到主题的 functions.php 即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * WordPress 注册表单添加验证问题
 * https://www.wpdaxue.com/add-a-security-question-to-the-register-screen.html
 */
add_action( 'register_form', 'add_security_question' );
function add_security_question() { ?>
	<p>
	<label><?php _e('中国的首都是哪里?') ?><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) {
	// 如果没有回答
	if (!isset($_POST[ 'user_proof' ]) || empty($_POST[ 'user_proof' ])) {
		return $errors->add( 'proofempty', '<strong>错误</strong>: 您还没有回答问题。'  );
	// 如果答案不正确
	} elseif ( strtolower( $_POST[ 'user_proof' ] ) != '北京' ) {
		return $errors->add( 'prooffail', '<strong>错误</strong>: 您的回答不正确。'  );
	}
}

/** * WordPress 注册表单添加验证问题 * https://www.wpdaxue.com/add-a-security-question-to-the-register-screen.html */ add_action( 'register_form', 'add_security_question' ); function add_security_question() { ?> <p> <label><?php _e('中国的首都是哪里?') ?><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) { // 如果没有回答 if (!isset($_POST[ 'user_proof' ]) || empty($_POST[ 'user_proof' ])) { return $errors->add( 'proofempty', '<strong>错误</strong>: 您还没有回答问题。' ); // 如果答案不正确 } elseif ( strtolower( $_POST[ 'user_proof' ] ) != '北京' ) { return $errors->add( 'prooffail', '<strong>错误</strong>: 您的回答不正确。' ); } }

注:第 8 行是问题,第 19 行是正确的答案。

如果你想限制用户注册,将问题和答案设置为只有某些人知道的即可(比如只在某个Q群告知)

2013-10-08 更新:

有朋友询问如何添加多个随机问题,感谢 @Rilun 提供了解决办法。

要实现多个随机问题,需要添加两个数组,分别存放着问题和答案,再增加一个两个函数都能取到的随机数(也就是在注册表单开始之前进行一个Session)即可。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * WordPress 注册表单添加验证问题(支持多个随机问题)
 * https://www.wpdaxue.com/add-a-security-question-to-the-register-screen.html
 */
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('中国的首都在哪里?','Google是哪个国家的公司?');
// 添加答案数组(与上面的问题对应)
$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[$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>: 您的回答不正确。' );
	}
}

/** * WordPress 注册表单添加验证问题(支持多个随机问题) * https://www.wpdaxue.com/add-a-security-question-to-the-register-screen.html */ 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('中国的首都在哪里?','Google是哪个国家的公司?'); // 添加答案数组(与上面的问题对应) $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[$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>: 您的回答不正确。' ); } }

注:请编辑 14 和 16 行修改问题和答案,如果你修改了问题的数量,请记得修改第 6 行的 随机数返回范围 rand(0,1) ,比如 3 个问题,修改为 rand(0,2) ,更多说明,请参考 rand() 函数文档。

本文WordPress 注册表单添加验证问题(支持多个随机问题)到此结束。生气是拿别人做错的事来惩罚自我。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress做公司官网好吗?会不会显得档次很低?

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?