Google reCAPTCHA:errors-captcha-sol错误

时间:2017-04-22 19:34:52

标签: php html forms recaptcha


我在使用谷歌验证码时遇到问题可能是php联系表格 我遵循Google官方指南,我写的代码似乎是正确的,但我总是收到不正确的验证码 - 错误。

客户端代码:

<div class="row">
    <div class="12u">
        <form id="mailform" method="post" action="website_mailer.php" novalidate="novalidate">
            <div>
                <div class="row half">
                    <div class="6u">
                        <input type="text" name="name" id="name" placeholder="Name" />
                    </div>
                    <div class="6u">
                        <input type="text" name="email" id="email" placeholder="Email" />
                    </div>
                </div>
                <div class="row half">
                    <div class="12u">
                        <input type="text" name="subject" id="subject" placeholder="Subject" />
                        <input type="text" class="myantispam" name="leaveblank">
                        <input type="text" class="myantispam" name="dontchange" value="http://" >
                    </div>
                </div>
                <div class="row half">
                    <div class="12u">
                        <textarea name="message" id="message" placeholder="Message"></textarea>
                    </div>
                </div>
                <div class="row half">
                    <div class="4u">
                        <div class="g-recaptcha" data-sitekey="xxxx"></div>
                    </div>
                    <div class="8u">
                        <a href="#" class="button form-button-submit">Send Message</a>
                        <a href="#" class="button button-alt form-button-reset">Clear Form</a>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

服务器端代码(PHP):

<?php
require_once('recaptchalib.php');

$privatekey = "xxx";
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
        die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
    } else {
      /* proper code */
    }
?>

你能建议我一个解决方案吗?我无法找到解决方案。

谢谢!

2 个答案:

答案 0 :(得分:2)

我使用以下代码

解决了这个问题
$response=json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET_KEY_HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
if($response['success'] == false){
    //Captcha incorrect
}
else {
    //Success code here
}

而不是

<?php
require_once('recaptchalib.php');

$privatekey = "xxx";
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
        die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
    } else {
      /* proper code */
    }
?>

感谢Locke Donohoe建议回答here

答案 1 :(得分:0)

在脚本目录的根目录下创建php.ini文件:

allow_url_fopen =开启