[PHP] 接口增加recaptcha行为验证

自作多情 提交于 2019-12-10 14:52:33

需要先翻墙创建一个谷歌账户和创建recaptcha验证的网站域名,获取到两个secrect
https://www.google.com/recaptcha/admin

 

前端增加html和js代码,例如

<input type="hidden" name="token" id="token" />
<script src="https://www.recaptcha.net/recaptcha/api.js?render=客户端scerect"></script>
<script>
grecaptcha.ready(function() {
    grecaptcha.execute('客户端scerect', {action: 'homepage'}).then(function(token) {
       $('#token').val(token);
    });
});
</script>

后端增加验证代码,例如:

post请求https://www.recaptcha.net/recaptcha/api/siteverify,
$tokenVerify=array();
$tokenVerify['secret']='服务端secrect';
$tokenVerify['response']=$params['token'];
$tokenResponse=post("https://www.recaptcha.net/recaptcha/api/siteverify", $tokenVerify);
if(empty($tokenArr)||!$tokenArr['success']){
    ...
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!