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


Validating google recaptcha by passing g-recaptcha-response to php file from a javascript file

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

首先是剥离的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="&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');
    }
?>

除了检查 recaptcha 是否已填充之外,一切都有效,我知道我在这里拥有的内容不会按原样工作,但我试图说明我的方法,我已经尝试了几个小时让它工作无济于事,任何帮助将不胜感激。希望有人能理解我正在做的事情,谢谢。

此链接显示第 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)