Javascript 提交Google Recaptcha后如何自动重定向?
我想用Google Recaptcha创建一个表单,但我不知道如何实现这个案例 这是我的HTML代码Javascript 提交Google Recaptcha后如何自动重定向?,javascript,php,html,recaptcha,Javascript,Php,Html,Recaptcha,我想用Google Recaptcha创建一个表单,但我不知道如何实现这个案例 这是我的HTML代码 <form action="redirect.php" method="post"> <div class="g-recaptcha" data-sitekey="xxxxxxxxxx"></div> <input type="submit" value="Submit" > </form> 如何在提交Google
<form action="redirect.php" method="post">
<div class="g-recaptcha" data-sitekey="xxxxxxxxxx"></div>
<input type="submit" value="Submit" >
</form>
如何在提交Google Recaptcha后重定向到页面?在javascript中,
google recaptcha有一个回调响应方法grecaptcha.getResponse()
。你可以检查它的长度是多少
>0
如果验证码成功
if(grecaptcha.getResponse().length==0){
//在这里重定向代码
}
阅读文档:
验证用户的响应
本页说明如何从应用程序后端验证用户对reCAPTCHA质询的响应。当最终用户解决reCAPTCHA时,将在HTML中填充一个新字段(g-reCAPTCHA-response)。您可以通过以下三种方式之一获取用户的响应:
当用户在您的站点上提交表单时,g-recaptcha-response POST参数
用户完成验证码质询后的grecaptcha.getResponse(opt_widget_id)
如果在g-recaptcha标记属性或grecaptcha.render方法中的回调参数中指定了数据回调,则将其作为回调函数的字符串参数
每个reCAPTCHA响应都是一个令牌,只能使用一次。如果使用特定令牌进行了验证尝试,则无法再次使用该令牌。您需要调用grecaptcha.reset()以要求最终用户再次使用reCAPTCHA进行验证
获取响应令牌后,需要使用以下API使用reCAPTCHA对其进行验证,以确保令牌有效
API请求
URL:https://www.google.com/recaptcha/api/siteverify
方法:POST
后参数说明
需要保密。站点和reCAPTCHA之间的共享密钥。
需要答复。reCAPTCHA提供的用户响应令牌,用于验证站点上的用户。
remoteip可选。用户的IP地址。
API响应
响应是一个JSON对象:
{
"success": true|false,
"challenge_ts": timestamp, // timestamp of the challenge load (ISO format yyyy-MM-dd'T'HH:mm:ssZZ)
"hostname": string, // the hostname of the site where the reCAPTCHA was solved
"error-codes": [...] // optional
}
PHP示例:
// set post fields
$post = [
'secret' => $secret,
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR']
];
$ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump(json_decode($response));
下面是一个使用reCaptchaV2的PHPV7.x的完整工作示例; 根据本页“meda”的回复、该页下的“活到死”以及Google reCaptcha v2自己的示例编写。我只是把这些碎片拼在一起,然后我就得到了。感谢贡献者
softlivre.com提供的reCAPTCHA V2演示。br