gpt4 book ai didi

javascript - 通过将 g-recaptcha-response 从 javascript 文件传递​​到 php 文件来验证 google recaptcha

转载 作者:行者123 更新时间:2023-11-27 23:17:37 25 4
gpt4 key购买 nike

我试图确保在发送包含我的表单数据的电子邮件表单之前填充 google recaptcha2。然而,由于我的网站运行需要基于 javascript 的电子商务,表单数据首先传递到 javascript 文件,然后作为另一种表单发布到 php 文件。我正在尝试找到一种方法来获取 g-recaptcha-response 并将其从原始表单一直获取到 javascript 表单再到 php sendmail 文件,以验证它是否已以正常后端方式填充。

首先是剥离的 html,这样您就可以看到我将所有内容都放在了正确的位置,站点 key 由“MySiteKey”补充:

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

<form name="form" id="form">
<fieldset>
<ol>
<li><label for="emaile">Email Address: </label>
<input type="email" placeholder="&nbsp;Email Address" name="emaile" class="input" required id="emaile" /></li>
</ol>

<div class="g-recaptcha" data-sitekey="MySiteKey"></div>
<button class="submit" type="submit" name="Submit" style="cursor:pointer" formaction="javascript:simpleCart.checkout()">Submit</button>
</fieldset>
</form>

接下来是外部 js 文件的大规模简化但相关的部分

simpleCart.checkout()

emaile = document.form.emaile.value;

var b = document.createElement("form");
b.style.display = "none";
b.method = "POST";
b.action = "/php/sendjs.php";
b.acceptCharset = "utf-8";

b.appendChild(a.createHiddenElement("emaile", emaile));

b.appendChild(a.createHiddenElement("g-recaptcha-response", g-recaptcha-response));

document.body.appendChild(b);
b.submit();
document.body.removeChild(b)

我想附加“g-recaptcha-response”字符串以与其他表单数据一起传递到单独的 php 文件,以在发送之前检查验证码是否已填充:

<?php
$captcha = $_POST['g-recaptcha-response'];
$secretKey = "MySecretSiteKey ";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify? secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
echo '<h2>NOT SENT</h2>';
} else {
$subject = 'Order Inquiry from DIY Soakwells';
$time = date ("h:i A");
$date = date ("l, F jS, Y");
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= 'From: order@DIYSoakwells.com' . "\r\n" .
'Reply-To: contact@diysoakwells.com.au' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

$emaile = $_POST['emaile'];

$to = "diysoakwells@hotmail.com,$emaile";
$text = "<html><body><p><b>Customers Email Address:</b> $emaile</p>
</html>
</body>";
$body = $text;
mail($to, $subject, $body, $headers);
Header('Location: ../form-accepted.php');
}
?>

除了检查验证码是否已填充之外,一切正常,我知道我这里的内容无法按原样工作,但我正在尝试说明我的方法,并且我已经尝试了几个小时才能使其正常工作有用,任何帮助将不胜感激。希望有人能理解我正在尝试做的事情,谢谢。

此链接显示第 2 部分中的键/值对 http://webdesign.tutsplus.com/tutorials/how-to-integrate-no-captcha-recaptcha-in-your-website--cms-23024

我非常愿意接受其他建议。

最佳答案

这应该可以解决所有问题。

function createHiddenElement(name, value) {
var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", name);
input.setAttribute("value", value);
return input;
}

simpleCart.checkout()


var b = document.createElement("form");
b.style.display = "none";
b.method = "POST";
b.action = "/php/sendjs.php";
b.acceptCharset = "utf-8";

b.appendChild(createHiddenElement('emails', document.form.emaile.value));
b.appendChild(createHiddenElement('g-recaptcha-response', document.getElementById('g-recaptcha-response').value));

document.body.appendChild(b);
b.submit();
document.body.removeChild(b)

关于javascript - 通过将 g-recaptcha-response 从 javascript 文件传递​​到 php 文件来验证 google recaptcha,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35699406/

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