新的Google ReCaptcha不发布/接收“ g-recaptcha-response” [英] New Google ReCaptcha not posting/receiving 'g-recaptcha-response'

查看:338
本文介绍了新的Google ReCaptcha不发布/接收“ g-recaptcha-response”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网站上实施Google的新 NoCaptcha。到目前为止,我的小部件看起来不错,但是无法在我的PHP页面上进行验证。

I am trying to implement Googles new 'NoCaptcha' on my site. So far I have the widget appearing fine, but it does not verify on my PHP page.

我的代码设置如下:

< 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>

服务器端(bookingverify.php)

Server Side (bookingverify.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。

I've tried echoing $_POST['g-recaptcha-response'] but it appears empty. It's like that variable is not being posted to the php.

有人知道我在这里做错什么吗?

Does anyone know what I am doing wrong here?

推荐答案

您的代码使用我自己的私钥/公钥在我的测试服务器上运行良好。看起来微不足道,但是我唯一要添加的内容-您在其他表单输入中是否有一个提交按钮?

Your code ran fine on my test server with my own private / public key. It seems trivial, but the only thing I had to add - do you have a submit button in your other form inputs? This is what actually posts the data to your PHP script.

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

否则,添加 var_dump($ _ POST ['g-recaptcha-response' ]); 到您的bookingverify.php并查看其输出。

Otherwise, add var_dump($_POST['g-recaptcha-response']); to your bookingverify.php and see what it outputs.

这篇关于新的Google ReCaptcha不发布/接收“ g-recaptcha-response”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆