首页 > 解决方案 > reCAPTCHA 仅返回 false 并且我在加载时收到 ERR_EMPTY_RESPONSE

问题描述

所以......我决定在我的网站上使用 reCAPTCHA,它在 JSON 解码时只返回 false,就像这样:

$secretKey = "***********";
$responseKey = $_POST['g-recaptcha-response'];

$url = 'https://www.google.com/recaptcha/api/siteverify? 
        secret='.$secretKey.'&response='.$responseKey.'';

$response = file_get_contents($url);
$response = json_decode($response);
if ($response->{'succes'} == 'true') {
  $_SESSION['response'] = "succes";
} else {
  $_SESSION['response'] = "fail";
}

它总是写“失败”

但是当它不是json解码时:

$secretKey = "**************";
$responseKey = $_POST['g-recaptcha-response'];

$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$responseKey.'');
$_SESSION['response'] = $response;

它返回真:{ "success": true, "challenge_ts": "2018-05-07T19:54:43Z", "hostname": "mywebsite.com" }

第二个问题是 ERR_EMPTY_RESPONSE 错误,当我加载操作页面时。仅当存在 reCAPTCHA 内容时才会发生。

我的帖子:

<form method="post" id="form" class="form" action="action/logreg.php">
 <input name="name" class="name" placeholder="Jméno" type="text" />
 <input name="surname" class="surname" placeholder="Přijmení" type="text"/>
 <input name="email" class="email" placeholder="E-mail" type="email"  />
 <input name="psswd" class="psswd" placeholder="Heslo"  type="password"/>
 <button type="submit"name="regBtn" class="regBtn">Registrovat</button>
 <div class="g-recaptcha" data-sitekey="*********"></div>
</form>

标签: phphtmlrecaptcha

解决方案


你有错字吗?

if($response->{'succes'} == 'true')

应该是成功的。此外,它是响应中的布尔值,而不是字符串,所以你不能放 'true',使用不带引号的true 。


推荐阅读