搞不懂为什么这不管用。当表单提交时,我收到错误消息,这意味着我的recaptcha验证失败。
在我的表单中:
<div class="g-recaptcha" data-sitekey="(site-key)"></div>
PHP:
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
$secretKey = "(secret-key)";
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) === true) {
echo '<h3>Thanks for your message!</h3>';
} else {
echo '<h3>Error</h3>';
}
发布于 2018-03-15 15:25:29
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
$recaptcha_secret = '(secret-key)';
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$captcha);
$response = json_decode($response, true);
if(!empty($response["success"]))
{
echo 'Thanks for your message!';
} else {
echo 'Error';
}
发布于 2020-04-12 01:03:01
尽管不推荐,但目前仍可以使用file_get_contents
来确认reCaptcha。我相信你的代码的问题出在下面的代码中:
if(intval($responseKeys["success"]) === true) {
echo '<h3>Thanks for your message!</h3>';
} else {
echo '<h3>Error</h3>';
}
删除intval()
,因为这是不需要的,并将布尔值转换为整数。===
还会比较类型,因此响应将始终转换为int,当然,与布尔型true
相比,也会转换为false。或者将true
更改为1
。所以它应该是:
if($responseKeys["success"] === true) {
echo '<h3>Thanks for your message!</h3>';
} else {
echo '<h3>Error</h3>';
}
https://stackoverflow.com/questions/43528882
复制相似问题