gpt4 book ai didi

php - 如何在 html 网络表单上添加 reCaptcha

转载 作者:太空宇宙 更新时间:2023-11-04 16:00:05 24 4
gpt4 key购买 nike

我的所有代码都准确地设置了 Google 如何记录 API,但它一直返回 bool 值 false,我做错了什么吗?

<?php
if(isset($_POST['sendEmail'])) {
$privateKey = "__my_secret_key__";
$response = $_POST['g-recaptcha-response'];
$remoteip = $_SERVER['REMOTE_ADDR'];
$url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$privateKey&response=$response&remoteip=$remoteip");

$result = json_decode($url, true);

if($result->success == true) {
$name = strip_tags(trim($_POST['name_1']));
$surname = strip_tags(trim($_POST['surname_1']));
$userArray = [
'name' => $name,
'surname' => $surname,
];
var_dump($userArray);

} else {
echo "reCaptcha failed, please try again...";
}
}

?>

以及我的 HTML 表单,其中包含从 Google 复制的 JavaScript 链接和我收到的数据站点 key :

<html lang="en">
<head>
<meta charset="utf-8">
<title>Recaptcha Example</title>
</head>

<body>
<br>
<br>
<form action="" method="post">
<div class="form-row">
<input name="name_1" placeholder="Your name" type="text">
</div>

<br>

<div class="form-row">
<input name="surname_1" placeholder="Your surname" type="text">
</div>

<br>

<div class="form-row">
<div class="g-recaptcha" data-sitekey="__Site_key__"></div>
</div>

<br>

<input name="sendEmail" type="submit" value="Call me back">

<br>

<h4 id="response"></h4>
</form>

<script src='https://www.google.com/recaptcha/api.js'></script>
</body>
</html>

最佳答案

添加这个脚本:

<script src='https://www.google.com/recaptcha/api.js'></script>
<script>

function get_action(form)
{
var v = grecaptcha.getResponse();
if(v.length == 0)
{
document.getElementById('captcha').innerHTML="You can't leave Captcha Code empty";
return false;
}
else
{
document.getElementById('captcha').innerHTML="Captcha completed";
return true;
}
}

</script>

并在您的提交按钮之前添加此 HTML:

<div class="g-recaptcha" id="rcaptcha"  data-sitekey="site key"></div>
<span id="captcha" style="color:red" /></span> <!-- this will show captcha errors -->

关于php - 如何在 html 网络表单上添加 reCaptcha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42271481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com