V
V
vovkka2018-02-20 08:51:50
PHP
vovkka, 2018-02-20 08:51:50

How to write a branch?

Good afternoon, tell me please, how would you do the error handling? I declare a variable with an error counter and an array of errors. What confuses me: below I say: if the counter is not equal to zero, then display errors, and if else, then the counter is equal to zero, but in the code below I also need to handle errors, so the counter increases there on certain branches. The question is whether it is normal to enter the branch by else (by condition, the counter is zero) and then increase it inside ... otherwise, how to handle errors? All the best!

if ($errorCount !== 0) {
      foreach ($errorMessage as $key => $value) {
        echo $value."<br>";
      }
    } else {

<?php
$errorMessage = [];
$errorCount = 0;
  if (isset($_POST["auth_submit"])) {

    //заносим введенный пользователем логин в переменную $login, если он пустой, то уничтожаем переменную
    if (trim($_POST["login"])) { 
      $login = trim($_POST['login']); 
      if (!preg_match("/^[a-zA-Z0-9_-]{3,20}$/", $login)) {
        $errorCount++;
        $errorMessage[] = "Логин содержит некорректные символы";
      } 
    } else {
      $errorCount++;
      $errorMessage[] = "Введите логин!";
      unset($login);
    }

    //заносим введенный пользователем пароль в переменную $password, если он пустой, то уничтожаем переменную 
    if (trim($_POST["password"])) {
      $password = trim($_POST['password']);
      if (!preg_match("/^[a-zA-Z0-9_-]{3,20}$/", $password)) {
        $errorCount++;
        $errorMessage[] = "Пароль содержит некорректные символы";
      }  
    } else {
      $errorCount++;
      $errorMessage[] = "Введите пароль!";
      unset($password);
    }

    
    if ($errorCount !== 0) {
      foreach ($errorMessage as $key => $value) {
        echo $value."<br>";
      }
    } else {
          //echo "Данные корректны";
          $login = trim($_POST['login']); 
          $password = trim($_POST['password']);

          //удаляем пользователей из двух таблиц, у которых истек срок годности проверки почты
          $query = "DELETE FROM players WHERE email_active = 0 AND date_registration < (NOW() - INTERVAL ".$timeRemoveNoActivePlayers.");"; 
          $result = dbDelete($query);

          $query = "DELETE FROM confirm_players WHERE date_registration < (NOW() - INTERVAL ".$timeRemoveNoActivePlayers.");"; 
          $result = dbDelete($query);

          $query = "SELECT * FROM players WHERE login = '$login';";

          $result = dbSelect($query);

          if (!$result) {
            $errorCount++;
            $errorMessage[] = "Пользователь с таким логином не найден!";
            //header("Location /");
          } 

          if ($result) {
            foreach ($result as $value) {
              $dbLogin = $value["login"];
              $dbPassword = $value["password"];
              $dbEmailActive = $value["email_active"];
            }

            if ($dbEmailActive) {
              if (password_verify($password, $dbPassword)) {
                $_SESSION["login"] = $dbLogin;
                header("Location: home.php"); 
                //echo "Добро пожловать!";
              } else {
                //echo "Неверный пароль!";
                $errorCount++;
                $errorMessage[] = "Неверный пароль!";
                //header("Location: /"); 
              }
            } else {
              $errorCount++;
              $errorMessage[] = "Вы не подтвердили свою почту!";
            }

          } 

          if ($errorCount !== 0) {
            foreach ($errorMessage as $key => $value) {
              echo $value."<br>";
            }
          }
      }
  } 

?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2018-02-20
@vovkka

The question is whether it is normal to enter the branch by else (by condition, the counter is zero) and then increase it inside ... otherwise, how to handle errors?

Fine. Why not? It is not recommended to change the array itself in a loop over an array, but in if-s - for the sake of the Buddha.
Well, in general, you often have to write something like: Which is essentially the same thing.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question