我正在尝试在一个包含2个表单的页面上使用2x reCapcha。我设法单独显示它们,现在我无法验证数据。要验证我使用两个单独的函数,如
$recaptcha = $_POST['g-recaptcha-response'];
if(!empty($recaptcha)) {
$google_url = "https://www.google.com/recaptcha/api/siteverify";
$secret = 'secret';
$ip = $_SERVER['REMOTE_ADDR'];
$url = $google_url."secret=".$secret."&response=".$recaptcha."&remoteip=".$ip;
$res = getCurlData($url);
$res = json_decode($res, true);
...
if($res['success']) { do stuff }
加载我使用的2个capchas
<script src="//www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>
<script type="text/javascript">
var recaptcha1;
var recaptcha2;
var myCallBack = function() {
//Render the recaptcha1 on the element with ID "recaptcha1"
recaptcha1 = grecaptcha.render('recaptcha1', {
'sitekey' : 'key', //Replace this with your Site key
'theme' : 'light'
});
//Render the recaptcha2 on the element with ID "recaptcha2"
recaptcha2 = grecaptcha.render('recaptcha2', {
'sitekey' : 'key', //Replace this with your Site key
'theme' : 'light'
});
};
</script>
有人可以帮助我,我该怎么办?这甚至可以吗?
答案 0 :(得分:0)
你做了一件棘手的事。
首先,您需要明确reCaptcha work是如何做到的。 现在,我将您引导至post,了解如何插入它并继续验证客户端。
<强> 1。在Google注册 - 这两个reCaptchas都有相同的网站密钥和密钥。
<强> 2。在您的网站上插入带有CAPTCHA的表单 - 您需要有一个包含recaptchas的表单, 2个不同的回调用于验证每个表单。
<script src="https://www.google.com/recaptcha/api.js" >;
<form method="post">
<div class="g-recaptcha" data-sitekey="[site_key]" data-callback="onReturnCallback1" data-theme="light"></div>
<div class="g-recaptcha" data-sitekey="[site_key]" data-callback="onReturnCallback2" data-theme="dark"></div>
<input value="submit" type="submit" />
</form>
第3。用于解码响应的JavaScript代码 - 您需要定义2个回调以分别验证每个reCaptcha。正如您所做的那样,您将验证结果存储在JS变量中。
<script> src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
var success1=null, success2=null;
var onReturnCallback1 = function(response) {
//alert('g-recaptcha-response: ' + grecaptcha.getResponse());
var url='proxy.php?url=' + 'https://www.google.com/recaptcha/api/siteverify';
$.ajax({ 'url' : url,
dataType: 'json',
data: { response: response},
success: function( data ) {
var res = data.success.toString();
if (res == 'true') {
success1=true;
if (success1&&success2) {
// do form submission
}
}
} // end of success:
}); // end of $.ajax
}; // end of onReturnCallback
</script>
在每次回调中,您可以保存成功success1
&amp; success2
并检查两者是否成功强制表单提交:
if (success1&&success2) {
// do form submission
}
<强> 4。服务器上的代理返回true或false - 似乎每个reCaptchas只需要一个代理文件来验证站点。
if (!isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
header('HTTP/1.0 403 Forbidden');
die('You are not allowed to access this file.');
}
header('Content-type: application/json');
$url=$_GET['url'];
$response=$_GET['response'];
$secret = "[secter_key]";
$params = array('secret'=> $secret, 'response'=> $response);
$json=file_get_contents( $url . '?secret=' . $secret . '&response=' . $response);
echo $json;