任何人都可以帮我清楚一下recaptcha文档的最后一页是什么,我发现它非常钝.
这是我无法理解的文档:
验证用户的响应
本页介绍了如何从应用程序的后端验证用户对reCAPTCHA质询的响应.当最终用户解决reCAPTCHA时,将在HTML中填充新字段(g-recaptcha-response).您可以通过以下三种方式之一验证用户的响应:
用户在您的站点上提交表单时的g-recaptcha-response POST参数.用户完成CAPTCHA质询后的grecaptcha.getResponse(opt_widget_id).如果在g-recaptcha标记属性或grecaptcha.render方法中的回调参数中指定了数据回调,则作为回调函数的字符串参数
API请求
网址:https: //www.google.com/recaptcha/api/siteverify?secret = your_secret&respondse = respondse_string&remoteote = user_ip_address
我究竟如何'验证'?
它说我可以通过三种方式"验证用户的响应",所以让我们采取第一种方式:现在提交的表单中有一个名为g-recaptcha-response的POST参数,带有一些gobbledygook内容.我的问题是:现在怎样?我只是检查它不是空的吗?
或者我是否可以使用下面提到的API请求将其发送到谷歌,然后检查他们的回复?这可能是有道理的,但如果文档拼写出来会很好,而只是说"API请求".如果他们拼写出response_string(可能是)g-recaptcha-response参数的内容,那也很好.
显然,我昂贵的教育费用不够昂贵,请有人确认我应该做的API申请让我高枕无忧.
这让我想到了第二个问题:你可以测试recaptcha小部件在本地机器上运行正常,但是你无法测试'API Request' - 我得到一个跨站点错误
XMLHttpRequest无法加载https://www.google.com/recaptcha/api/siteverify.请求的资源上不存在"Access-Control-Allow-Origin"标头.
http://localhost因此,不允许访问" 来源" .
有人知道解决这个问题的方法,以便你可以做测试吗?
也许这篇文章会有所帮助,因为它显示了来自后端和前端预测的确切代码片段:
http://www.codedodle.com/2014/12/google-new-recaptcha-using-javascript.html
PHP代码:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Construct the Google verification API request link.
$params = array();
$params['secret'] = 'Your secret key here.'; // Secret key
if (!empty($_POST) && isset($_POST['g-recaptcha-response'])) {
$params['response'] = urlencode($_POST['g-recaptcha-response']);
}
$params['remoteip'] = $_SERVER['REMOTE_ADDR'];
$params_string = http_build_query($params);
$requestURL = 'https://www.google.com/recaptcha/api/siteverify?' . $params_string;
// Get cURL resource
$curl = curl_init();
// Set some options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $requestURL,
));
// Send the request
$response = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
$response = @json_decode($response, true);
if ($response["success"] == true) {
echo '<h3 class="alert alert-success">Login Successful</h3>';
} else {
echo '<h3 class="alert alert-danger">Login failed</h3>';
}
}
Run Code Online (Sandbox Code Playgroud)
从您所说的看来,您的主要问题是您在用户的浏览器而不是服务器上验证用户的响应.真的吗?
只是为了澄清,会发生什么......
response_string,在你的表单中可用作字段g-recaptcha-response(你也可以使用他们提到的其他两个javascript方法获得它).response_string所有其他表单数据.| 归档时间: |
|
| 查看次数: |
8414 次 |
| 最近记录: |