关于php:Google reCAPTCHA:captcha-sol错误

Google reCAPTCHA: incorrect-captcha-sol error

我在5月php联络表格中使用Google验证码时遇到问题。
我遵循了Google官方指南,我写的代码似乎是正确的,但是我总是收到不正确的captcha-sol错误。

客户端代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
        <form id="mailform" method="post" action="website_mailer.php" novalidate="novalidate">
           
               
                   
                        <input type="text" name="name" id="name" placeholder="Name" />
                   
                   
                        <input type="text" name="email" id="email" placeholder="Email" />
                   
               
               
                   
                        <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://">
                   
               
               
                   
                        <textarea name="message" id="message" placeholder="Message"></textarea>
                   
               
               
                   
                       
                   
                   
                        Send Message
                        Clear Form
                   
               
           
        </form>

服务器端代码(PHP):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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 */
    }
?>

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

谢谢!


我使用以下代码解决了该问题

1
2
3
4
5
6
7
$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
}

而不是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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)在这里提出了答案。


在脚本的根目录中的

使用以下命令创建php.ini文件:

allow_url_fopen =开启