mercredi 13 février 2019

PHP executes the first block of "if" anyway

I've been trying to make a login system with only one login&password (just for fun), and it needs some "if"s, but when I use it, it executes the first block and anyway, even if the condition is false. I don't know what's wrong with the code, I checked the syntax — it's looks as it should. Tried Firefox and Yandex browser, doesn't work.
Here, check it out, please. Thanks.

<form method=post>
<label for=login>Login:</label><br>
<input type=text id=login><br>
<label for=password>Password:</label><br>
<input type=password id=password><br>
<input type=submit value=Submit>
</form>
<?php
$log_err = $pass_err = $all_err = '';
$logged = 'nope';
if ($_POST['login'] = 'ann25099' && $_POST['password'] = '<!--cut off my password, it doesn't matter-->') {
  echo '<h1><b>Welcome home, Ann!</b></h1>';
  echo '<meta http-equiv="Refresh" content="1; logged.php">';
  $logged = 'yep';
}
elseif (empty($_POST['login'])) {
  $log_err = 'Please, enter your login.';
}
elseif (empty($_POST['password'])) {
  $pass_err = 'Please, enter your password.';
}
elseif (empty($_POST['login']) && empty($_POST['password'])) {
  $log_err = 'Please, enter your login.';
  $pass_err = 'Please, enter your password.';
}
elseif (!empty($_POST['login']) && $_POST['login'] != 'ann25099') {
  $log_err = 'Wrong login has been entered.';
}
elseif (!empty($_POST['password']) && $_POST['password'] != '<!--password was here-->') {
  $log_err = 'Wrong password has been entered.';
}
elseif (!empty($_POST['login']) && $_POST['login'] != 'ann25099' && !empty($_POST['password']) && 
$_POST['password'] != '<!--password-->') {
  $log_err = 'Wrong login has been entered.';
  $pass_err = 'Wrong password has been entered.';
}
else {
  $all_err = 'Something went wrong. Please, ask me <a href="mailto:ann25099@yandex.ru">here</a>.';
}
?>
<p style="color:red;"><?php echo "$log_err"; ?></p>
<p style="color:red;"><?php echo "$pass_err"; ?></p>
<p style="color:red;"><?php echo "$all_err"; ?></p>

If there was any grammar mistakes/misspellings, sorry for that. I'm not a native. Hope you understand what I mean.

Aucun commentaire:

Enregistrer un commentaire