我试图确保在发送包含我的表单数据的电子邮件表单之前填充google recaptcha2。然而,由于基于JavaScript的电子商务需要我的网站的功能,表单数据首先传递给一个JavaScript文件,然后作为另一种形式发布到PHP文件。我试图找到一种方法来使用g-recaptcha-response,并从原始表单到javascript表单到php sendmail文件,以验证它已经以普通的后端方式填充。通过从JavaScript文件传递g-recaptcha-response到php文件来验证google recaptcha
首先剥离HTML,所以你可以看到我在正确的地方的一切,网站的重点由“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=" 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-验证码 - 响应”字符串传递给同其他形式的数据的单独的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: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$emaile = $_POST['emaile'];
$to = "[email protected],$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');
}
?>
一切工作,除了检查recaptcha是填充,我明白我在这里不会工作的方式,但我试图说明我的方法,我一直在努力几个小时才能让它工作到无济于事,任何帮助将不胜感激。希望有人能够理解我正在尝试做什么,谢谢。
的键/值对我非常开放的替代的建议。
“https://www.google.com/recaptcha/api/siteverify?secret”< - 删除空格? – Kordi
不幸的是,这不是问题,当我粘贴问题的代码时可能有空白。验证检查在php文件中工作,我只是不知道如何获取键/值对,以允许它发送。它在谷歌浏览器(js文件)的检查员中说“g-recaptcha-response未定义”。 – JPB