Google Recaptcha v2 Invisible 根本不工作说缺少输入响应
Google Recatpcha v2 Invisible not working at all says missing-input-response
我浏览了 Stack 和 google 上的各种文章,并在 Codeigniter 3 中完成了以下实现。我的网站是 运行 over https。
头部代码
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmit(token) {
document.getElementById("enquiryform").submit();
}
</script>
表格代码
<?php
echo form_open('SubmitForm', array('id'=>'enquiryform'));
?>
<div class="row">
<div class="col-sm-12 col-md-12">
<div class="form-group">
<textarea type="text" class="form-control" rows="5" cols="3" name="message" placeholder="Message"></textarea>
</div>
<div class="text-center contact-page-button">
<button class="g-recaptcha btn btn-danger border-0" data-sitekey="my_site_key" data-callback='onSubmit'>Submit</button>
<input type="reset" class="btn btn-danger border-0"></input>
</div>
</div>
</div>
<?php
echo form_close();
?>
控制器函数中的代码
$this->load->library('curl');
if($this->input->post('g-recaptcha-response'))
{
$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
$userIp=$this->input->ip_address();
$secret='my_secret_key';
$url="https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response;=".$recaptchaResponse."&remoteip;=".$userIp;
$response = $this->curl->simple_get($url);
$status= json_decode($response, true);
//print_r($recaptchaResponse); //prints the data
//echo "<br>";
//print_r($url); //prints the data
//echo "<br>";
//print_r($status); //prints the data
//echo "<br>";
if($status['success'] == 1) {
//True - EXECUTE SOMETHING
} else {
//False - What happens when user is not verified
$data['response']="<div class='alert alert-danger'>Failed to verify that you are a Human. Please Try again.</div>";
}
我什至试过file_get_contents方法,但结果是一样的。我总是
Array ( [success] => [error-codes] => Array ( [0] => missing-input-response ) )
我已经多次检查了站点和密钥,它们是正确的。我什至为 g-recaptcha 放置了一个单独的 div 来显示复选框版本,但我仍然遇到相同的错误。
解决方法是什么?
问题出在您的服务器端代码中。您需要发送 POST 请求而不是 GET。
$url="https://www.google.com/recaptcha/api/siteverify";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,"secret=".$secret."&response=".$recaptchaResponse."&remoteip=".$userIp);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
我浏览了 Stack 和 google 上的各种文章,并在 Codeigniter 3 中完成了以下实现。我的网站是 运行 over https。
头部代码
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script>
function onSubmit(token) {
document.getElementById("enquiryform").submit();
}
</script>
表格代码
<?php
echo form_open('SubmitForm', array('id'=>'enquiryform'));
?>
<div class="row">
<div class="col-sm-12 col-md-12">
<div class="form-group">
<textarea type="text" class="form-control" rows="5" cols="3" name="message" placeholder="Message"></textarea>
</div>
<div class="text-center contact-page-button">
<button class="g-recaptcha btn btn-danger border-0" data-sitekey="my_site_key" data-callback='onSubmit'>Submit</button>
<input type="reset" class="btn btn-danger border-0"></input>
</div>
</div>
</div>
<?php
echo form_close();
?>
控制器函数中的代码
$this->load->library('curl');
if($this->input->post('g-recaptcha-response'))
{
$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
$userIp=$this->input->ip_address();
$secret='my_secret_key';
$url="https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response;=".$recaptchaResponse."&remoteip;=".$userIp;
$response = $this->curl->simple_get($url);
$status= json_decode($response, true);
//print_r($recaptchaResponse); //prints the data
//echo "<br>";
//print_r($url); //prints the data
//echo "<br>";
//print_r($status); //prints the data
//echo "<br>";
if($status['success'] == 1) {
//True - EXECUTE SOMETHING
} else {
//False - What happens when user is not verified
$data['response']="<div class='alert alert-danger'>Failed to verify that you are a Human. Please Try again.</div>";
}
我什至试过file_get_contents方法,但结果是一样的。我总是
Array ( [success] => [error-codes] => Array ( [0] => missing-input-response ) )
我已经多次检查了站点和密钥,它们是正确的。我什至为 g-recaptcha 放置了一个单独的 div 来显示复选框版本,但我仍然遇到相同的错误。
解决方法是什么?
问题出在您的服务器端代码中。您需要发送 POST 请求而不是 GET。
$url="https://www.google.com/recaptcha/api/siteverify";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,"secret=".$secret."&response=".$recaptchaResponse."&remoteip=".$userIp);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);