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

标签 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. <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0b160f031e020b2e0a01030f0700400d0103" rel="noreferrer noopener nofollow">[email protected]</a>" />
    <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>

Submit使用POST方法调用actionsubmit-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.";
    }
?>

关于php - Google reCaptcha - 空错误消息,未收到对质询的响应(reCAPTCHA 未正确输入),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35250474/

相关文章:

html - 'li' + float 使 Internet Explorer 6 崩溃

captcha - 本地网络计算机中的reCAPTCHA

php - 对 MySQL 查询的 PHP 结果进行“分组”

javascript - 如何使用 jQuery 在页面加载时关注表单输入文本字段?

php - 从现有 PHP 网站创建 iPhone 应用程序有哪些选项?

python - 如何使用 Python BeautifulSoup 将输出写入 html 文件

css - 调整大小 无验证码 重新验证码

asp.net - https ://api-secure. recaptcha.net/上的 recaptcha https 问题

php - php select prepared 语句是什么样的?

php - 创建自定义 PHP 语法分析器