Google reCaptcha - 空错误消息,未收到对质询的响应(reCAPTCHA 未正确输入)

Google reCaptcha - empty error message, not receiving response to challenge (The reCAPTCHA wasn't entered correctly)

编辑:我正在使用 reCaptcha 2.0,我不应该使用 recaptchalib.php 吗?

现场演示 http://josiahbertoli.com/

提交表单时出现以下错误(忽略输入字段的空白值)(带有调试代码):

错误

_POST: =========
Array
(
    [first_name] => 
    [last_name] => 
    [email] => 
    [subject] => 
    [comments] => 
    [g-recaptcha-response] => big-long-value
)

=========

reCAPTCHA 输入不正确。回去再试一次。(reCAPTCHA said: )`

我的网页是如何设置的:

HTML

<form id="query-form" action="wp-content/themes/portfolio/submit-form.php" method="post" name="myForm">
    <input id="first_name" name="first_name" size="35" type="text" placeholder="e.g. John" />
    <input id="last_name" name="last_name" size="35" type="text" placeholder="e.g. Smith" />
    <input id="email" name="email" size="35" type="text" placeholder="e.g. example@domain.com" />
    <input id="subject" name="subject" size="35" type="text" placeholder="e.g. Feedback" />
    <textarea id="comments" name="comments"></textarea>
    <div class="g-recaptcha" data-sitekey="6Le8WxcTAAAAAGqymotU9wtOBFEmWgjM3j2kqTcB"></div>

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

提交使用方法POST调用动作提交-form.php如下

PHP

<?php
require_once('recaptchalib.php'); 
$privatekey = "the-key-that-i-put-in"; 
echo "<pre> _POST: =========\n"; print_r($_POST); echo "\n=========\n</pre>";
$resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], 
    $_POST["recaptcha_challenge_field"], 
    $_POST["recaptcha_response_field"]); 
$resp = null;
if (!$resp->is_valid) { 
// What happens when the CAPTCHA was entered incorrectly 
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")"); } 
    else { 
        // Your code here to handle a successful verification 
        include '../../../../process.php';
    } 

?>

感谢您的回复,我很感激。

为了显示 reCAPTCHA 小部件,您不需要任何库文件,您只需包含必要的 JavaScript 资源,如下所示:

<html>
  <head>
    <title>reCAPTCHA demo: Simple page</title>
     <script src="https://www.google.com/recaptcha/api.js" async defer></script>
  </head>
  <body>
    <form action="?" method="POST">
      <div class="g-recaptcha" data-sitekey="your_site_key"></div>
      <br/>
      <input type="submit" value="Submit">
    </form>
  </body>
</html>

引用如下:

现在是您的用户响应。由于您使用的是 Google reCAPTCHA V2,因此您应该使用 POST 参数 g-recaptcha-response.

获取用户的响应

引用如下:

所以你的代码应该是这样的:

<?php   
    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){
        $privatekey = "YOUR_PRIVATE_KEY";

        //get verified response data
        $param = "https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".$_POST['g-recaptcha-response'];
        $verifyResponse = file_get_contents($param);
        $responseData = json_decode($verifyResponse);

        if($responseData->success){
            // success
            echo "success";

        }else{
            // failure
            echo "failure";
        }
    }else{
        // user didn't enter reCAPTCHA
        echo "The reCAPTCHA wasn't entered correctly. Go back and try it again.";
    }
?>