WordPress注册用户添加验证码

建站教程 阅读

WordPress注册用户添加验证码,我这种小博客基本很少用到给网友注册用户的,登录后台添加验证码,找回密码添加验证码文章都写了,也不差这个注册用户添加验证码文章了,大概差不多只需要一串代码就行了,也不用安装任何插件就能现实验证码功能,主要的目的是防止浪费主机资源和防止机器人捣蛋,今天分享6种验证码功能总有你喜欢的一种验证码。

把以下全部代码丢进模板函数 (functions.php)

/*-----------------------------------------------------------------------------------*/
/* WordPress注册用户随机数字验证码
/*-----------------------------------------------------------------------------------*/
# 注册用户添加验证码第一种
function loper_register_english_figures() {
# 获取两个随机数, 范围0~99
$num1=rand(0,99);
$num2=rand(0,99);
echo "<p>
<label for='math' class='small'>验证码:$num1 + $num2 = ? </label>
<input id='math' type='text' name='sum' class='input' size='25'>
<input type='hidden' name='num1' value='$num1'>
<input type='hidden' name='num2' value='$num2'></p>";
}
add_action('register_form','loper_register_english_figures');
# 判断验证码是否空白和错误
function loper_register_calculation() {
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']+$_POST['num2']:break;
case null:wp_die( __( '错误:请填入验证码!' ), '', array( 'back_link'=>true ) );break;
default:wp_die( __( '错误:验证码不正确!' ), '', array( 'back_link'=>true ) );}
}
add_action( 'register_post', 'loper_register_calculation');


把以下全部代码丢进模板函数 (functions.php)

/*-----------------------------------------------------------------------------------*/
/* WordPress注册用户随机数字验证码
/*-----------------------------------------------------------------------------------*/
# 注册用户添加验证码第二种
function loper_register_english_figures() {
# 获取两个随机数, 范围0~99
$num1=rand(0,99);
$num2=rand(0,99);
echo "<p>
<label for='math' class='small'>验证码</label>
<input id='math' type='text' name='sum' class='input' size='25' placeholder='$num1 + $num2 = ? '>
<input type='hidden' name='num1' value='$num1'>
<input type='hidden' name='num2' value='$num2'></p>";
}
add_action('register_form','loper_register_english_figures');
# 判断验证码是否空白和错误
function loper_register_calculation() {
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']+$_POST['num2']:break;
case null:wp_die( __( '错误:请填入验证码!' ), '', array( 'back_link'=>true ) );break;
default:wp_die( __( '错误:验证码不正确!' ), '', array( 'back_link'=>true ) );}
}
add_action( 'register_post', 'loper_register_calculation');


把以下全部代码丢进模板函数 (functions.php)

/*-----------------------------------------------------------------------------------*/
/* WordPress注册用户随机混合数验证码
/*-----------------------------------------------------------------------------------*/
# 注册用户添加验证码第三种
function loper_register_english_figures() {
# 获取英语和数字随机数, 范围0~99,目前是0,5【5位数】
$num1=substr(md5(mt_rand(0,99)),0,5);
echo "<p>
<label for='math' class='small'>验证码:$num1 </label>
<input id='math' type='text' name='sum' class='input' size='25'>
<input type='hidden' name='num1' value='$num1'></p>";
}
add_action('register_form','loper_register_english_figures');
# 判断验证码是否空白和错误
function loper_register_calculation() {
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']:break;
case null:wp_die( __( '错误:请填入验证码!' ), '', array( 'back_link'=>true ) );break;
default:wp_die( __( '错误:验证码不正确!' ), '', array( 'back_link'=>true ) );}
}
add_action( 'register_post', 'loper_register_calculation');


把以下全部代码丢进模板函数 (functions.php)!

/*-----------------------------------------------------------------------------------*/
/* WordPress注册用户随机数字验证码
/*-----------------------------------------------------------------------------------*/
# 注册用户添加验证码第四种
function loper_register_english_figures() {
# 获取两个随机数, 范围0~99
$num1=rand(0,99);
$num2=rand(0,99);
echo "<p>
<label for='math' class='small'>验证码</label>
<input id='math' type='text' name='sum' class='input' size='25' placeholder='$num1 + $num2 = ? '>
<input type='hidden' name='num1' value='$num1'>
<input type='hidden' name='num2' value='$num2'></p>";
}
add_action('register_form','loper_register_english_figures');
# 判断验证码是否空白和错误
function loper_register_calculation($login, $email, $errors) {
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']+$_POST['num2']:break;
case null:$errors->add( 'zlinet', "<strong>错误</strong>:请输入验证码!" ); break;
default:$errors->add( 'zlinet', "<strong>错误</strong>:验证码不正确!" );}
}
add_action( 'register_post', 'loper_register_calculation', 9, 9);


把以下全部代码丢进模板函数 (functions.php)

/*-----------------------------------------------------------------------------------*/
/* WordPress注册用户随机数字验证码
/*-----------------------------------------------------------------------------------*/
# 注册用户添加验证码第五种
function loper_register_english_figures() {
# 获取两个随机数, 范围0~99
$num1=rand(0,99);
$num2=rand(0,99);
echo "<p>
<label for='math' class='small'>验证码:$num1 + $num2 = ? </label>
<input id='math' type='text' name='sum' class='input' size='25'>
<input type='hidden' name='num1' value='$num1'>
<input type='hidden' name='num2' value='$num2'></p>";
}
add_action('register_form','loper_register_english_figures');
# 判断验证码是否空白和错误
function loper_register_calculation($login, $email, $errors) {
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']+$_POST['num2']:break;
case null:$errors->add( 'zlinet', "<strong>错误</strong>:请输入验证码!" ); break;
default:$errors->add( 'zlinet', "<strong>错误</strong>:验证码不正确!" );}
}
add_action( 'register_post', 'loper_register_calculation', 9, 9);


把以下全部代码丢进模板函数 (functions.php)

/*-----------------------------------------------------------------------------------*/
/* WordPress注册用户随机混合数验证码
/*-----------------------------------------------------------------------------------*/
# 注册用户添加验证码第六种
function loper_register_english_figures() {
# 获取英语和数字随机数, 范围0~99,目前是0,5【5位数】
$num1=substr(md5(mt_rand(0,99)),0,5);
echo "<p>
<label for='math' class='small'>验证码:$num1 </label>
<input id='math' type='text' name='sum' class='input' size='25'>
<input type='hidden' name='num1' value='$num1'></p>";
}
add_action('register_form','loper_register_english_figures');
# 判断验证码是否空白和错误
function loper_register_calculation($login, $email, $errors) {
$sum=$_POST['sum'];
switch($sum){
case $_POST['num1']:break;
case null:$errors->add( 'zlinet', "<strong>错误</strong>:请输入验证码!" ); break;
default:$errors->add( 'zlinet', "<strong>错误</strong>:验证码不正确!" );}
}
add_action( 'register_post', 'loper_register_calculation', 9, 9);

把以下全部代码丢进模板函数 (functions.php)

//WordPress新用户注册随机数学验证码
function add_security_question_fields() {
//获取两个随机数, 范围0~9
$num1=rand(0,9);
$num2=rand(0,9);
//最终网页中的具体内容
echo "<p><label for='math' class='small'>验证码:$num1 + $num2 = ? </label><input type='text' name='sum' class='input' value='' size='25'>"
."<input type='hidden' name='num1' value='$num1'>"
."<input type='hidden' name='num2' value='$num2'></p>";}
add_action('register_form','add_security_question_fields');
add_action( 'register_post', 'add_security_question_validate', 10, 3 );
function add_security_question_validate( $sanitized_user_login, $user_email, $errors) {
$sum=$_POST['sum'];//用户提交的计算结果
switch($sum){
//得到正确的计算结果则直接跳出
case $_POST['num1']+$_POST['num2']:break;
//未填写结果时的错误讯息
case null:wp_die('错误:请输入验证码!');break;
//计算错误时的错误讯息
default:wp_die('错误:验证码错误,请重试!');}}
add_action( 'add_security_question','register_form' );

来源:https://www.zlinet.com/11860.html

本文链接:https://niujc.com/com/1368999.html

栏目:建站教程
来源:
标签:wordpress
时间:2022-06-25

晚上好!当前时间为
目前距离2023年春节还有
TOP