javascript 蜜罐实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16861325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Honeypot implementation
提问by blackessej
Trying to filter out spam from an online form. I have a hidden div with an input. The idea is that if something goes into the field, the form will ID the user as a bot and reject the submission. After trying to implement this method, the bots are still getting through. I'm not very familiar with javascript (or spam-filtration, for that matter) - here's what I'm working with:
试图从在线表单中过滤掉垃圾邮件。我有一个带有输入的隐藏 div。这个想法是,如果有东西进入该字段,表单会将用户标识为机器人并拒绝提交。在尝试实施此方法后,机器人仍然通过。我对javascript(或垃圾邮件过滤,就此而言)不是很熟悉 - 这是我正在使用的:
html (within the form):
html(在表单内):
<form action="#" method='post' id='vsurvey' name='defer'>
<div id="hp-div">
If you see this, leave this form field blank
and invest in CSS support.
<input type="text" name="question_20579" value="" />
</div>
<input type="submit" value="Submit Request" />
</form>
css:
css:
#hp-div { display: none }
js:
js:
<script type="text/javascript" charset="ISO-8859-1" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" charset="ISO-8859-1" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript">
if(!String.IsNullOrEmpty(Request.Form["question_20579"]))
IgnoreComment();
</script>
<![if !IE]>
<script type="text/javascript">
$(document).ready(function(){
$("#vsurvey").validate({
invalidHandler: function(form, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
var message = errors == 1
? 'Oops! You missed 1 field. It has been highlighted'
: 'Oops! You missed ' + errors + ' fields. They have been highlighted below';
$("div.alert span").html(message);
$("div.alert").show();
} else {
$("div.alert").hide();
}
},
errorPlacement: function(error, element) {
return true;
}
})
});
</script>
<![endif]>
回答by rybo111
In my opinion, a honeypot should consist of ALL of the below:
在我看来,蜜罐应该包括以下所有内容:
- A field hidden by CSS
- A field hidden by JavaScript
- A field requiring a blank input
- A field requiring a specific input
- CSS 隐藏的字段
- JavaScript 隐藏的字段
- 需要空白输入的字段
- 需要特定输入的字段
For instance:
例如:
<div class="input-field">
Please leave this blank
<input type="text" name="contact" value="" />
</div>
<div class="text-field">
Please do not change this field
<input type="text" name="email" value="[email protected]" />
</div>
Using CSS, hide the first field:
使用 CSS,隐藏第一个字段:
.input-field { display: none; }
Using jQuery, hide the second field:
使用 jQuery,隐藏第二个字段:
$('.text-field').hide();
// or
$('.text-field').addClass('hide');
Then a couple of very simple checks in PHP:
然后在 PHP 中进行一些非常简单的检查:
if($_POST['contact'] == '' && $_POST['email'] == '[email protected]') {
// Not a bot
}

