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

时间:2016-02-07 06:28:48

标签: php html recaptcha

编辑:我使用的是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说道)``

我的网页设置方式:

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来调用动作submit-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';
    } 

?>

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

1 个答案:

答案 0 :(得分:3)

要显示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.";
    }
?>