首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >未使用file_get_contents验证Recaptcha

未使用file_get_contents验证Recaptcha
EN

Stack Overflow用户
提问于 2017-04-21 04:03:58
回答 2查看 16.5K关注 0票数 11

搞不懂为什么这不管用。当表单提交时,我收到错误消息,这意味着我的recaptcha验证失败。

在我的表单中:

代码语言:javascript
复制
<div class="g-recaptcha" data-sitekey="(site-key)"></div>

PHP:

代码语言:javascript
复制
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>';
    }
EN

回答 2

Stack Overflow用户

发布于 2018-03-15 15:25:29

代码语言:javascript
复制
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';  
}  
票数 1
EN

Stack Overflow用户

发布于 2020-04-12 01:03:01

尽管不推荐,但目前仍可以使用file_get_contents来确认reCaptcha。我相信你的代码的问题出在下面的代码中:

代码语言:javascript
复制
if(intval($responseKeys["success"]) === true) {
    echo '<h3>Thanks for your message!</h3>';
} else {
    echo '<h3>Error</h3>';
}

删除intval(),因为这是不需要的,并将布尔值转换为整数。===还会比较类型,因此响应将始终转换为int,当然,与布尔型true相比,也会转换为false。或者将true更改为1。所以它应该是:

代码语言:javascript
复制
if($responseKeys["success"] === true) {
    echo '<h3>Thanks for your message!</h3>';
} else {
    echo '<h3>Error</h3>';
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43528882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档