Pregunta

Estoy creando un formulario de comentario emergente JQuery Ajax, pero estoy teniendo un problema con la forma en que estoy configurando mi "Honeypot" en PHP.

El honeypot ($ robotest) no funciona;En su lugar, el script devuelve "el correo electrónico no es correcto".¿Alguien puede señalar mi error?Gracias

El formulario HTML es:

<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>

Editar:

Gracias a @jamwaffles para el apoyo.A continuación se muestra la forma correcta de implementar el honeypot.(Y, como señaló Kamalo, querrá tener el ID de 'robotest' set para mostrar: Ninguno en su 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";
}   
?>

¿Fue útil?

Solución

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 = "")) 

You're executing code inside the if() when filter_var() fails, which is why you're getting

E-mail is not correct

for valid emails.


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 == "")) 

Otros consejos

For an alternative answer, I set up a "honey pot" input in my html with display:none

<input type="text" name="honeypot" id="honeypot" style="display:none;"/>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top