Honeypot PHP的评论表 [英] Honeypot PHP for Comment Form
问题描述
我创建一个jquery ajax弹出注释表单,但我有一个问题,我在PHP中设置我的蜜罐。
I am creating a jquery ajax popup comment form, but am having a problem with the way Im setting up my "honeypot" in php.
蜜罐($ robotest)不工作;而是脚本返回电子邮件不正确。任何人都可以指出我的错误?谢谢
The honeypot ($robotest) isn't working; instead the script returns "E-mail is not correct". Can anyone point out my error? Thank you
html表单是:
<form class="cmxform" id="commentForm" method="POST" action="">
<p>
<label for="cname">Name</label>
<input id="cname" name="name" size="25" class="required" minlength="2" />
</p>
<p>
<label for="cemail">E-Mail</label>
<input id="cemail" name="email" size="25" class="required email" />
</p>
<p>
<label for="curl">URL</label>
<input id="curl" name="url" size="25" class="url" value="" />
</p>
<p>
<label for="ccomment">Your comment</label>
<textarea id="ccomment" name="comment" cols="22" class="required"></textarea>
</p>
<p class="robotic" id="pot">
<label>Please leave this blank:</label>
<input name="robotest" type="text" id="robotest" class="robotest" />
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
编辑:
感谢@JamWaffles为支持。下面是实现蜜罐的正确方法。 (和Kamalo指出,你会想要的'robotest'的ID设置为显示:在你的CSS中没有):
Thanks to @JamWaffles for the support. Below is the correct way to implement the honeypot. (And as Kamalo noted you will want to have the id of 'robotest' set to display:none in your css):
<?php
$robotest = $_POST['robotest'];
$email = $_POST['email'];
if((!filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest == "")) {
print "E-mail is correct";
$to = 'asdfdsafasdfsda@gmail.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com';
mail($to, $subject, $message, $headers);
} else {
print "E-mail is not correct";
}
?>
推荐答案
filter_var()
返回一个非falsy值有效,不是 false
。移除 if()
中的 filter_var(
)之前的!
:
filter_var()
returns a non-falsy value when the email is valid, not false
. Remove the !
before filter_var(
in your if()
:
if((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest = ""))
您正在 if()
当 filter_var()
失败,这就是为什么你会得到
You're executing code inside the if()
when filter_var()
fails, which is why you're getting
电子邮件不正确
E-mail is not correct
有效的电子邮件。
我错过的还有一个事实,就是你分配给 $ robotest
你的 if()
看起来应该是这样:
Something else I missed too is the fact you're assigning to $robotest
instead of comparing it against an empty string. You need to use the double equals comparison operator instead of the single equals assignment operator. Your if()
should look like this:
if((filter_var($email, FILTER_VALIDATE_EMAIL)) && ($robotest == ""))
这篇关于Honeypot PHP的评论表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!