Hello, there is a MVC site model, the meaning is that if a person is authorized to have his session, the controller returns to the address "/ admin" the admin panel. If not, it returns an authorization form. The problem is that when I log in, I need to refresh the page manually, so that the admin panel would appear if you use header( "Location: /admin" ); then throws an error - Warning: Cannot modify header information - headers already sent by What to do?

Controller Code:

 class AdminController { public function actionPanel() { if (isset($_SESSION['users-login'])) { $client = AdminPanel::getSelectClientList(); include_once ROOT . '/views/layaut/administrator/Panel.php'; }else{ include_once ROOT . '/views/layaut/administrator/Login.php'; } return true; } } 

Authorization code:

 <?php include_once ROOT . '/views/scripts/scripts.html'; include_once ROOT . '/views/views/administrator/Login.html'; $login = $_POST; $errors = array(); if (isset($login['do_admin'])){ $log = AdminPanel::getEntrance('users', $login['email'], $login['pass']); if ($log == false){ $errors[] = 'Неверно введено имя пользователя или пароль'; } if(empty($errors)){ $_SESSION['users-login'] = $log; header( "Location: /admin" ); }else { echo '<div style="color: #ff4930;">' .array_shift($errors).'</div>'; } } 

HTML form itself:

 <div class="container" id="authorization"> <div class="col-12 col-md-4"> <form action="\admin" method="post" class="form-signin"> <h2 class="form-signin-heading">Авторизация</h2> <label for="inputEmail" class="sr-only">Адрес электронной почты</label> <input name='email' type="email" id="inputEmail" class="form-control" placeholder="Адрес электронной почты" required="" autofocus=""> <label for="inputPassword" class="sr-only">пароль</label> <input name='pass' type="password" id="inputPassword" class="form-control" placeholder="Пароль" required=""> <div class="checkbox"> </div> <button class="btn btn-lg btn-primary btn-block" type="submit" name="do_admin">Вход</button> </form> </div> </div> 

    1 answer 1

    You do not need to display anything on the screen before header() , otherwise it will not work. You must delete the following:

     include_once ROOT . '/views/scripts/scripts.html'; include_once ROOT . '/views/views/administrator/Login.html'; 

    Then everything will work.