![](/img/trans.png)
[英]Can submit contact form without google recaptcha v2 validation
[英]Google reCAPTCHA V2 checkbox HTML and PHP Contact Form Code
我過去曾使用該網站尋找答案,並決定創建一個帳戶,以便我可以分享和幫助他人。 這篇文章不是一個問題,而是分享我為我創建的幾個公司網站使用的代碼。 我們從我們的聯系表格中收到了很多垃圾郵件,因此我實施了“蜜罐”、reCAPTCHA 和其他方法來消除傳入的垃圾郵件。 下面是我的 HTML 代碼和 PHP 代碼,供任何為此苦苦掙扎的人使用。 到目前為止,自從實施此操作以來,我還沒有收到任何垃圾郵件。 所有這些代碼都是由我在本網站和互聯網上的其他網站上找到的信息創建的。
HTML 代碼:(蜜罐輸入“網站”隱藏在 CSS 中)
<form id="form" action="php/submit-en.php" method="post">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>Name</label>
<input class="form-control form-control-name" name="the-name" id="pccn" autocomplete="off" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" placeholder="Required" type="text" maxlength="31" required>
<script>
$(function() {
$("#pccn").keyup(function() {
console.log(this.value);
this.value = this.value.replace(/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/mg, 'Links Not Accepted!');
})
});
</script>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Email</label>
<input class="form-control form-control-email" name="the-email" id="pcemail" autocomplete="off" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" placeholder="Required" type="email" maxlength="32" required>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>Subject</label>
<input class="form-control form-control-subject" name="the-subject" id="pcsubject" autocomplete="off" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" placeholder="Optional" maxlength="20" type="text">
<script>
$(function() {
$("#pcsubject").keyup(function() {
console.log(this.value);
this.value = this.value.replace(/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/mg, 'Links Not Accepted!');
})
});
</script>
<label class="website">
<input class="blind" name="website" type="text" id="website" placeholder="Website Link:" tabindex="-1" autocomplete="off" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false"/>
</label>
</div>
</div>
</div>
<div class="form-group">
<label>Message</label>
<textarea class="form-control form-control-message" name="the-info" id="pcinfo" autocomplete="off" onpaste="return false" onCopy="return false" onCut="return false" onDrag="return false" onDrop="return false" placeholder="Required" rows="10" maxlength="2048" required></textarea>
<script>
$(function() {
$("#pcinfo").keyup(function() {
console.log(this.value);
this.value = this.value.replace(/(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?/mg, 'Links Not Accepted!');
})
});
</script>
<div class="text-right">
<br>
<div class="g-recaptcha" data-sitekey="YOUR SITE KEY"></div>
<button class="btn-primary" data-callback='onSubmit' data-action='submit'>Send Message</button>
</div>
</div>
</form>
我在 HTML 中添加了代碼以拒絕表單中的任何 Web 鏈接,然后復制/粘貼。 這些不是高使用率的網站,所以我不太擔心人們必須在表單上手動輸入。
PHP 代碼:(提交-en.php)
<?php
if ( $_SERVER['REQUEST_METHOD']=='GET' && realpath(__FILE__) == realpath( $_SERVER['SCRIPT_FILENAME'] ) ) {
header( 'HTTP/1.0 404 Page not found', TRUE, 404 );
die( header('location: /no-spam.html') );
}
if(!isset($_GET)){
header('Location: /no-spam.html');
}
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo 'Please check the the captcha form.';
exit;
}
$secretKey = "YOUR SECRET KEY";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if($responseKeys["success"]) {
echo 'Thanks for posting your message';
} else {
header('Location: /no-spam.html');
}
// if the url field is empty, but the message field isn't
if(isset($_POST['website']) && $_POST['website'] == '' && $_POST['the-info'] != ''){
// CONTACT INFO
$name = $_REQUEST['the-name'];
$subject = $_REQUEST['the-subject'];
$email = $_REQUEST['the-email'] ;
$message = $_REQUEST['the-info'];
$ip = $_SERVER['REMOTE_ADDR'];
// ASSEMBLE HEADERS
$ouremail = "email@yourwebsite.com";
$subject1 = "Website Contact Form Submission";
$subject2 = "Website Submission Received";
$headers = "From: $ouremail\r\n";
$headers .= "Reply-To: 'email@yourwebsite.com'\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= 'Content-type:text/html;charset=utf-8' . "\r\n";
// E-MAIL MESSAGE TO YOU
$message = "
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
<title>Contact Form Submission</title>
</head>
<body>
<h3 style='color: #0D773C;'>Website Submission:</h3>
<p>
<strong>Name:</strong> $name <br>
<strong>Subject:</strong> $subject <br>
<strong>Email:</strong> $email <br>
<strong>IP Address:</strong> $ip <br>
</p>
<p>
<strong>Message:</strong></p>
<p style='font-style:italic';>$message
</p>
</body>
</html>
";
// SEND MAIL
mail($ouremail,$subject1,$message,$headers);
// E-MAIL MESSAGE TO CUSTOMER
$message2 = "
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
<title>Contact Form Submission Received</title>
</head>
<body>
<h3 style='color: #0D773C;'>Thank you for contacting us!</h3>
<p style='font-style:italic';>Thank you for contacting Your Company Inc.<br>
We have received your message, and will get back to you as soon as possible.<br>
If your message is of an urgent nature, please call us during normal business hours.</p>
<p style='font-style:italic';>
Sincerely,<br><br>
Your Company Inc.<br>
Tel: 123-456-7890</p>
<p>www.yourwebsite.com</p>
</body>
</html>
";
// SEND MAIL
mail($email,$subject2,$message2,$headers);
header('Location: /thankyou.html');
exit('Redirecting you to /thankyou.html');
}
當直接通過 Web 瀏覽器訪問時,PHP 的第一部分停止訪問 submit-en.php 文件。 如果第二部分收到 GET 請求,則重定向到無垃圾郵件頁面。 其余的驗證驗證碼是否已被檢查,然后驗證響應。 然后它檢查蜜罐是否已裝滿。 如果是這樣,它不會發送電子郵件,而是重定向到感謝頁面,因此垃圾郵件發送者認為電子郵件已通過。 其余的會向您發送格式精美的電子郵件並回復客戶。
使用這個出色的腳本時,我發現提交表單時收到的消息帶有前綴 [SPAM}。
然后我更改為另一個托管帳戶,雖然這刪除了 {SPAM} 前綴,但表單將不再重定向到thank.html 頁面。
經過一番調查,我發現通過將php文件中的倒數第二行代碼更改為:
echo "<script type='text/javascript'> document.location = 'thankyou.html'; </script>";
表單現在再次重定向到“謝謝”頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.