簡體   English   中英

通過將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是否已裝滿,我了解我在這里無法按原樣工作,但我試圖說明自己的方法,並且我一直在嘗試數小時以使其無法工作,任何幫助將不勝感激。 希望有人能理解我要做什么,謝謝。

此鏈接在第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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM