【问题标题】:google recaptcha response is null [PHP, localhost]google recaptcha 响应为空 [PHP, localhost]
【发布时间】:2017-06-22 14:37:21
【问题描述】:

在转移到开放互联网之前,我需要一些帮助来解决我正在使用我的电脑(所以是本地主机)开发的网站中的 Google Recaptcha 问题。

我注册了 Google Recaptcha 并获得了一对密钥。我在一个 php 页面中创建了这个表单:

<div>
  <form action="" method="POST" name="formEmail">
    <section>
      <ul class="formMail" id="ulMsg">
        <li>
          <label for="msg">Messagge</label>
        </li><li>
          <textarea class="datoForm autoExpand" name="msg" id="msg" placeholder="Type Msg" rows='3' data-min-rows='3'></textarea>
        </li>
      </ul>
    </section>

    <div class="formMail" id="captchaContainer">
      <div class="g-recaptcha" data-sitekey="[Public_Key]"></div>
    </div>
    <br/><input type="button" value="Invia" onclick="formSubmit();">                            
  </form>
</div>

我调用一个 JS 文件来验证用户输入,而不是提交按钮,如果一切正常,我将数据提交到另一个检查验证码的 php 页面。这个php页面的来源是:

if(isset($_POST['g-recaptcha-response'])){$captcha=$_POST['g-recaptcha-response'];}
$secretKey = "[Private_Key]";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);

问题是:我什么都没有!我试过了

var_dump($responseKeys);

但我得到的只是 NULL。我没有收到任何其他错误,验证码在表单中显示良好,并且似乎可以正常工作。我在 localhost 工作,所以我的 IP 是 127.0.0.1,这应该有帮助但没用。我没有“打开”的网站来粘贴它并尝试,我做错了什么?

【问题讨论】:

    标签: php forms validation recaptcha


    【解决方案1】:

    刚刚遇到同样的问题。这是一个&lt;div&gt; 标签导致了问题。

    我的表单位于主&lt;div&gt; 中,该&lt;div&gt; 用于格式化表单的总体布局。 &lt;form&gt; 标签不必在我用于表单布局的主要 &lt;div&gt; 内。我在表单布局 &lt;div&gt; 标记之前移动了 &lt;form&gt; 标记,它开始完美运行。

    &lt;table&gt; 标签也会发生同样的情况。您需要在用于格式化表单的任何表格之前启动 &lt;form&gt; 标记。

    所以你的问题是&lt;div&gt; 就在&lt;form&gt; 之前:

    <div> <form action="" method="POST" name="formEmail">

    只需颠倒两个标签就可以了:

    <form action="" method="POST" name="formEmail"> <div>

    【讨论】:

      【解决方案2】:

      根据Google recaptcha documentation

      方法:POST

      但是您在这一行发送GET 请求:

      $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
      

      已经提出了解决方案here

      【讨论】:

      • 首先感谢您的回复。我在互联网上的许多教程中发现了该代码,因此我认为它有效。不幸的是,我尝试了您建议的解决方案,但仍然无法正常工作...
      【解决方案3】:

      在 HTML 标签内:

      <head>
      <script src="https://www.google.com/recaptcha/api.js"></script>
      </head>
      
      <body>
      
      <form action="" method="POST">
      
      <div class="g-recaptcha" data-sitekey="PublicKey"></div>
      
      <input type="submit" name="submit" value="Post comment">
      
      </form>
      
      </body>
      

      PHP 内部标签:

      <?php
      
      if(isset($_POST['submit'])) {
          if( isset($_POST['g-recaptcha-response']) && !empty( $_POST['g-recaptcha-response'] ) ) {
              $secret = 'PrivateKey';
              $verifyResponse=file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
              $responseData = json_decode($verifyResponse);
              var_dump($responseData); //this line returns Null Value
      
              if($responseData->success) {
                  // Logical Code
              }
              else {
                  echo 'Robot verification failed, please try again.';
              } 
          }
      }
      
      ?>
      

      【讨论】:

        猜你喜欢
        • 2019-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-22
        • 2018-10-28
        • 2015-11-05
        • 2018-10-28
        • 1970-01-01
        相关资源
        最近更新 更多