PHP logic error in construct if elseif else

2

I have a simple e-mail form with reCaptcha.

To prevent the user from sending the email and getting F5 and sending the same email several times, I made a redirect in a php script. But there is a logic error in this script, because even though both variables are fake it is sending the email anyway!

Note that the structure of the script should be ( if variable == false, elseif, else and not if variable == true, else if , else as usual), because to redirect the page I change the php header and this should be the first thing to do before giving print commands (print, echo, html outside the script).

Here is my code, note that it prints both fake variables and prints "all ok".

<?php

$ok_post = false;
$ok_captcha = false;

echo "ok_post = ", (int)$ok_post;
echo "<br>ok_captcha = ", (int)$ok_captcha;

if ((!ok_post && !ok_captcha) || (!ok_post && ok_captcha)) {

  echo '<br>Formulário vazio.';

} elseif (!ok_captcha && ok_post) {

  echo '<br>Captcha vazio.';

} else {

  echo '<br>Tudo ok!';

}

?>

output:

ok_post = 0
ok_captcha = 0
Tudo ok!

EDIT: The following python script works as it should, so I came to the conclusion that I'm using some PHP operator wrong, but I do not know which one.

ok_post = False
ok_captcha = False

print 'ok_post', ok_post
print 'ok_captcha', ok_captcha

if not ok_post and not ok_captcha or not ok_post and ok_captcha:
  print 'formulario vazio'
elif not ok_captcha and ok_post:
  print 'captcha vazio'
else:
  print 'ok'

output:

ok_post False
ok_captcha False
formulario vazio
    
asked by anonymous 17.06.2015 / 16:26

1 answer

6

Your error was simple, you did not set $ of variáveis at check in if and elseif .

Script :

<?php

$ok_post = false;
$ok_captcha = false;

echo "ok_post = ", (int)$ok_post;
echo "<br>ok_captcha = ", (int)$ok_captcha;

if ((!$ok_post && !$ok_captcha) || (!$ok_post && $ok_captcha)) {

  echo '<br>Formulário vazio.';

} elseif (!$ok_captcha && $ok_post) {

  echo '<br>Captcha vazio.';

} else {

  echo '<br>Tudo ok!';

}

?>
    
17.06.2015 / 16:34