Page 1 of 1

=== V != question

Posted: Fri Jun 02, 2006 5:16 pm
by Addos
Why does this work:-

Code: Select all

$spammed = trim(strtolower($_POST['StopSpam']));
if ($spammed === 'wednesday' && !empty($_POST['StopSpam'])) {
}else{
   $nospam = 'Error: You must answer the question';
 }
And this doesn’t (or at lease seems to work in reverse!)

Code: Select all

$spammed = trim(strtolower($_POST['StopSpam']));
if ($spammed != 'wednesday' && !empty($_POST['StopSpam'])) {
}else{
   $nospam = 'Error: You must answer the question';
 }
I have a form where a question is asked before the form is submitted and I need to run the above conditional statement.
Thanks

Posted: Fri Jun 02, 2006 5:25 pm
by Christopher
Uh ... because not equal is the opposite of equals?

Posted: Fri Jun 02, 2006 5:30 pm
by feyd
"!=" does almost the exact opposite of "===." Maybe you meant to use "=="?

Posted: Fri Jun 02, 2006 5:42 pm
by Christopher
And in you case, because trim() returns a string type you only need ==

Code: Select all

$spammed = trim(strtolower($_POST['StopSpam']));  // trim() returns a string
if ($spammed == 'wednesday' && !empty($_POST['StopSpam'])) {     // $spammed and 'wednesday' are both the same type

Posted: Fri Jun 02, 2006 5:57 pm
by Addos
Thanks for this help.

Why I was puzzled is that I was getting a lot of spam hitting my forms even though I was setting up a form field that needed to have human intervention in order for the form to submit. For example http://www.conor-cummins.com/guestbook_sign.php Now, as the form was been hit despite my attempts to keep things at bay I was directed to http://www.geekministry.com/blog_article.php?id=40 and when I looked at the code I found

Code: Select all

(ifstrtolower(trim($_POST['antispam'])) != "seven")
which I thought was very similar to mine. I appreciate that != is different to === but I wondered if != was any different a conditional statement to use to stop my forms being hit by spam. As I have limited knowledge of PHP I just thought that my bit of code (ie the very first one in the post above with ===) really carried out the same function but as this method was recommended in another newsgroup I decided to investigate it a little more.

Any ideas
Thanks again
Brian
Ps I know about captchas but I find it very confusing to implement and I know it have its accessibility issues to name but a few!

Posted: Fri Jun 02, 2006 6:28 pm
by Christopher
Just you are clear, the difference between ==/!= and ===/!== is that the latter also check that both values are the same type. In your case all vars you get from the request (GET/POST) will be strings so that type of check is of little use in this instance.

Here is a list of Captcha software in PHP. You may find one that you can get to work for you. There are many others around the net as well.