reCAPTCHA仅返回false,我在加载时收到ERR_EMPTY_RESPONSE

时间:2018-05-07 20:04:15

标签: php html recaptcha

所以...我决定在我的网站上使用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;

返回true:{ "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>

1 个答案:

答案 0 :(得分:1)

你有拼写错误?

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

它应该成功。 此外,它在响应中是一个布尔值,而不是字符串,因此您无法在没有引号的情况下使用 true

相关问题