php - 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>
解决方案
你有错字吗?
if($response->{'succes'} == 'true')
应该是成功的。此外,它是响应中的布尔值,而不是字符串,所以你不能放 'true',使用不带引号的true 。
推荐阅读
- flutter - Iterator current is null, but why?
- firebase - Merging streams together to create paginated ListView
- r - 从R中的Word文档中提取项目符号
- batch-file - 如何精确选择数字的一部分作为简单字符并在 CMD 中排除所有其余部分?
- jquery - 仅当动态创建的类匹配时拖放
- visualization - Pyvista 已经是一个完整的库了吗?[2021年1月]
- python - 使用 BeautifulSoup 4 引用 Html 标签内的两个类
- java - 如何在 JUnit 5 中使用 JUnitCore 运行参数化测试?
- javascript - 如何查看 React Router v6 中的查询字符串是否发生变化?
- mysql - 在 SQL_sever 中打开 MYSQL json_array 数据