新的谷歌验证码没有发布/接收“g-recaptcha-response”


New Google ReCaptcha not posting/receiving 'g-recaptcha-response'

我正在尝试在我的网站上实现谷歌的新'NoCaptcha'。到目前为止,我的小部件看起来很好,但它没有在我的 PHP 页面上验证。

我的代码设置如下:

<head>

<script src='https://www.google.com/recaptcha/api.js'></script>

客户端:

<form id="contactform" action="bookingverify.php" method="POST">
<input type="text" name="name" size="41">
<!--OTHER FORM INPUTS-->
<div class="g-recaptcha" data-sitekey="mypublickey"></div>
</form>

服务器端(预订验证.php)

  $captcha;
  if(isset($_POST['g-recaptcha-response'])){
       $captcha=$_POST['g-recaptcha-response'];
     }
  if(!$captcha){
      echo '<h2>Please check the the captcha form.</h2>';
       exit;
   }
  $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=myprivatekey&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
  if($response.success==false){
          echo '<h2>You are spammer</h2>';
  }
  else{
     //SEND MAIL
  }

我尝试回显$_POST['g-recaptcha-response']但它似乎是空的。就像该变量没有发布到 php 中一样。

有谁知道我在这里做错了什么?

您的代码使用我自己的私钥/公钥在我的测试服务器上运行良好。这似乎微不足道,但我唯一要补充的是 - 您的其他表单输入中是否有提交按钮?这就是实际将数据发布到PHP脚本的原因。

<input type="submit">
</form>

否则,请将var_dump($_POST['g-recaptcha-response']);添加到您的预订验证中.php并查看其输出的内容。

好的,我不知道

为什么,但我删除了第二个 IF 语句中的exit;并且它起作用了。奇怪。

我也有类似的问题。显然,如果您不单击复选框,$_POST['g-recaptcha-response'] 将为空。因此,请确保在测试时单击它