我们有一个PHP表单,在reCaptcha上有多个选项卡和超时。一切都在一页上完成,如果表单在3分钟内完成,则可以很好地工作。
解决方案的想法是将表单处理和reCaptcha移至辅助页面进行处理。
问题在于表单页面会在Google服务中轮询reCaptcha,并将令牌值收集到隐藏字段中。
<input type="hidden" name="recaptcha_response" id="recaptchaResponse">
问题是如何在服务器端处理页面上请求此令牌?这是客户端表单页面上使用的代码。我需要以某种方式重新生成令牌值以应用为:
$ recaptcha_response
这是表单页面上的工作版本。从表单页面上发布令牌的要求很容易删除,只是不确定如何重新生成令牌以在服务器端页面上使用。
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['recaptcha_response'])) {
// Build POST request:
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_secret = RECAPTCHA_SECRET_KEY;
$recaptcha_response = $_POST['recaptcha_response'];
$remoteip = $_SERVER['REMOTE_ADDR'];
// Make and decode POST request:
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response. '&remoteip='.$remoteip);
$recaptcha = json_decode($recaptcha);
// Take action based on the score returned:
if ($recaptcha->score >= 0.5) {
编辑添加:将reCaptcha初始化,直到Submit延迟计时问题,因为这似乎是一个选择:
https://developers.google.com/recaptcha/docs/v3
“ 2.在操作或页面加载时调用grecaptcha.execute”
我们最近也一直在碰到这个问题。我在GitHub上找到了该解决方案,并且对我们来说运作良好。
这样的好处是,仅当用户与页面进行交互(例如提交表单)时才要求令牌,而不必继续要求一个。
<script>
grecaptcha.ready(function() {
document.getElementById('contactform').addEventListener("submit", function(event) {
event.preventDefault();
grecaptcha.execute('mykey', {action: 'homepage'}).then(function(token) {
document.getElementById("googletoken").value = token;
document.getElementById('contactform').submit();
});
}, false);
});
</script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句