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

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

security-question-wpdaxue_com

将下面的代码添加到主题的 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() 函数文档。

本人擅长Ai、Fw、Fl、Br、Ae、Pr、Id、Ps等软件的安装与卸载,精通CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal等单词的拼写,熟悉Windows、Linux、OS X、Android、iOS、WP8等系统的开关机。

通过下面的方式来联系我们:

电邮:138762189@qq.com

联系QQ:点击这里给我发消息

官方站:www.tadke.com

※ ※ 联系请加我的企鹅号 ※※

※ ※技术支持请微信联系站长 ※※

Copyright © 2023 Tadke.com. 琼ICP备20000547号