wordpress注册加上问题验证
functions.php 文件的最后加上下面的代码
/**
* 验证码测试
*
*/
function rand_reg_question(){
$register_number=rand(0,1); // 设置随机数的返回范围 ,如果有三个问题,修改为0,2以此类推
if(!isset($_SESSION)){
session_start();
}
$_SESSION['register_number']=$register_number;
}
add_action('login_head','rand_reg_question');
global $register_questions;
global $register_answers;
$register_questions=array('树上4只鸟,开了一枪打死一只还剩多少鸟?(数字)','小明的有3兄弟,大个叫大明。二哥叫二明,三哥叫什么?');// 可以继续添加问题和答案 ,注意格式即可
$register_answers=array('0','小明');
add_action( 'register_form', 'add_security_question' );
function add_security_question() {
global $register_questions;
$register_number=$_SESSION['register_number'];
echo "<p>";
echo "<label>". $register_questions[$register_number]."<br />";
echo "<input type='text' name='user_proof' id='user_proof' class='input' size='25' tabindex='20' />";
echo "</label>";
echo "</p>";
}
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>: 您的回答不正确。' );
}
}
来自于datou:https://github.com/datou-leo/ci