美文网首页
wordpress无插件纯代码实现注册验证 拒绝恶意注册

wordpress无插件纯代码实现注册验证 拒绝恶意注册

作者: Happyhome | 来源:发表于2018-03-30 17:22 被阅读0次

wordpress能否实现注册验证功能?

最近楚狂人博客改版,花在网站上的心思多了一些,发现每天都有很多注册通知邮件发到我的邮箱里来,有点不胜其扰,哥还要专注分享免费资源和零投资网上赚钱的信息呢,哪有时间应付这些恶意注册者呢?应该找个方法给wordpress添加验证功能了。

试用的第一个方法是通过一个验证码插件,略繁琐,需要先安装geetest插件。然后还要去插件的官网注册账号,并获得密钥,回来进行插件设置之后才可以用,关键是也不知道我设置的有问题,还是需要付费,反正启用插件后前台注册时验证码总是出错,这样正常的注册会员也被挡住了,不行。

那么能否通过代码实现wordpress注册验证呢?

经过一番搜索还真找到了,把下面这段代码添加到自己的functions.php,其中的验证问题我使用了本站的域名“chukuangren.com”,你也可以改成其他内容。注意改的时候,代码第四行和倒数第四行都有,要改两处哦。

add_action('register_form','add_security_question');function add_security_question(){?>

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','错误: 您还没有回答问题。');// 如果答案不正确} elseif ( strtolower( $_POST['user_proof'])!='chukuangren.com'){return $errors->add('prooffail','错误: 您的回答不正确。');}}

效果:

注册验证

如果你觉得一个问题不过瘾,wordpress还可以实现注册随机验证:

session_start();

function rand_reg_question(){

$register_number=rand(0,2); // 设置随机数的返回范围

$_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(‘北京’,’美国’,’wordpress’);

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随机注册验证的朋友可以自行测试一下,祝你成功!

相关文章

网友评论

      本文标题:wordpress无插件纯代码实现注册验证 拒绝恶意注册

      本文链接:https://www.haomeiwen.com/subject/zyiicftx.html