reCaptcha无法正常工作;它不会返回成功 [英] reCaptcha isn't working; it doesn't return success

查看:149
本文介绍了reCaptcha无法正常工作;它不会返回成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要填写验证码,并且在服务器端集成方面遇到一些麻烦。



该表单采用四种类型的数据:


  1. name

  2. 电子邮件

  3. 评论。

确保没有一个为空后,我要验证验证码。但是,由于某种原因,它总是返回 success == false



有人可以帮我发现我的代码有什么问题吗?

 函数validate($ data)
{
$ data = trim($ data);
$ data = stripslashes($ data);
$ data = htmlspecialchars($ data);
返回$ data;
}

$ nameMsgErr = $ emailErr = $ msgSuccess = $ error =;

if(!empty($ _ POST ['name_msg'])&&!empty($ _ POST ['email'])&&!empty($ _ POST ['subject'] )&&!empty($ _ POST ['message'])){

$ url ='https://www.google.com/recaptcha/api/siteverify';
$ private_key =‘------私人密钥--------’;

$ response = file_get_contents($ url。?secret =。$ private_key。& response =。$ _HOST ['g-recaptcha-response']。& remoteip = 。$ _SERVER ['REMOTE_ADDR']);

$ data = json_decode($ response);

if(isset($ data-> success)AND $ data-> success == true){

$ name = validate($ _ POST ['name_msg' ]);
$ email = validate($ _ POST [’email’]);
if(!filter_var($ email,FILTER_VALIDATE_EMAIL)){
$ emailErr =电子邮件格式错误;
} else {

$ subject = validate($ _ POST [’subject’]);
$ msg = validate($ _ POST [’message’]);
$ msg。= \r\n。 $ name;
$ msg =自动换行($ msg,70, \r\n);
$ header =发件人:。 $ email;

mail( myemail9@gmail.com,$ subject,$ msg,$ header);

$ msgSuccess =消息发送成功;

}
} else {
$ error =错误;
}
}


解决方案

您使用错误的HTTP方法来验证用户响应。在您的代码中,您使用 file_get_contents 并发送GET请求,每次都返回false。



如上所述在文档中,您需要向Google recaptcha api发送POST请求。 / p>

有关使用<$ c发送POST请求的信息,请参见答案$ c> file_get_contents



注意:cURL是发送POST请求的更常用的方法,可能更易于掌握和实现。我建议使用cURL开始。



编辑(添加了特定示例,未经测试):

  $ postdata = http_build_query(
array(
'secret'=> $ private_key,
'response'=> $ _HOST [ g-captcha-response ],
'remoteip'= >> $ _SERVER [ REMOTE_ADDR]

);
$ opts = array('http'=>
array(
'method'=>>'POST',
'header'=>'Content-type: application / x-www-form-urlencoded',
'content'=> $ postdata

);

$ context = stream_context_create($ opts);

$ response = file_get_contents(‘https://www.google.com/recaptcha/api/siteverify’,FALSE,$context);


I need a captcha for my form, and I am having some troubles with the server-side integration.

The form takes in four types of data:

  1. name
  2. email
  3. comment.

After making sure that none of them are empty, I want to verify the captcha. However, for some reason, it always returns success == false.

Can somebody help me spotting what's wrong with my code?

function validate($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

$nameMsgErr = $emailErr = $msgSuccess = $error = "";

if(!empty($_POST['name_msg']) && !empty($_POST['email']) && !empty($_POST['subject']) && !empty($_POST['message'])) {

    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $private_key = '------Private Key--------';

    $response = file_get_contents($url . "?secret=" . $private_key . "&response=" . $_HOST['g-recaptcha-response'] . "&remoteip=" . $_SERVER['REMOTE_ADDR']);

    $data = json_decode($response);

    if(isset($data->success) AND $data->success == true) {

        $name = validate($_POST['name_msg']);
        $email = validate($_POST['email']);
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Wrong email format";
        } else {

            $subject = validate($_POST['subject']);
            $msg = validate($_POST['message']);
            $msg .= "\r\n" . $name;
            $msg = wordwrap($msg, 70, "\r\n");
            $header = "From: " . $email;

            mail("myemail9@gmail.com", $subject, $msg, $header);

            $msgSuccess = "Message successfully sent";

        }
    } else {
        $error = "Error";
    }
}

解决方案

You are using the wrong HTTP method to verify the user response. In your code, you use file_get_contents and it is sending a GET request, which returns false everytime.

As stated in the documentation, you need to send a POST request to the google recaptcha api.

See this answer on sending a POST request using file_get_contents

Note: cURL is the more common method of sending POST requests, and may be much simpler to grasp and implement. I would suggest using cURL to start.

Edit (Added specific example, not tested):

$postdata = http_build_query(
    array(
        'secret'    =>  $private_key,
        'response'  =>  $_HOST["g-recaptcha-response"],
        'remoteip'  =>  $_SERVER["REMOTE_ADDR"]
    )
);
$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', FALSE, $context); 

这篇关于reCaptcha无法正常工作;它不会返回成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆