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

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

我试图确保在发送包含我的表单数据的电子邮件表单之前填充 google 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 是否已填充,一切正常无济于事,我们将不胜感激。希望有人能理解我正在尝试做的事情,谢谢。

此 link 显示第 2 部分中的 key/value 对 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)