![](/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.