This question has already been answered:

Good day. I do not understand what the error is in the code. In the browser, an error occurs when redirecting to another file - Cannot modify header information - headers already sent by. Explain where I nakosyachil? I searched the Internet for information, I understood about the essence of the error, but I don’t know how to fix it. Already the html code was deleted, and I looked at all the spaces with empty lines, I can not understand what the problem is.

<!DOCTYPE HTML> <html> <meta charset="utf-8"> </html> <?php if (isset($_POST['login'])) { $login = $_POST['login']; } if (isset($_POST['password'])) { $password=$_POST['password']; } $login = stripslashes($login); $login = htmlspecialchars($login); $password = stripslashes($password); $password = htmlspecialchars($password); $login = trim($login); $password = trim($password); include ("bd.php"); $result = mysql_query("SELECT * FROM users WHERE login='$login'",$db); $myrow = mysql_fetch_array($result); if (empty($myrow['password'])) { exit ("Извините, введённый вами login или пароль неверный."); } else { if ($myrow['password']==$password) { $_SESSION['login']=$myrow['login']; $_SESSION['id']=$myrow['id']; //echo "Вы успешно вошли на сайт! <a href='index.php'>Главная страница</a>"; header('Location: http://localhost/practice/main.php/'); exit; } else { exit ("Извините, введённый вами login или пароль неверный."); } } ?> 

Reported as a duplicate by andreymal members, Community Spirit Apr 10 '18 at 6:48 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    Simply transfer the output before the <?php tag in your example after the code. That is, first execute the code, decide what will be on the page, and only then output when you have already decided and considered everything - wirtwelt
  • Transfer the entire php code up your file, and note that before the opening <?php tag there are no spaces or empty lines. All HTML should be located under php. - Edward
  • @Eduard Transfer php code up the file, there are no empty lines and spaces. HTML below, nothing has changed unfortunately :( - SnoopDoggyDog
  • Then check the file encoding - must be UTF-8 without BOM - Edward

1 answer 1

PHP uses output buffering to display the result of the script.

The usual HTTP protocol consists of two conditional sections: HTTP headers and the body of the page. PHP usually generates the body of the page, but header management is also available to it, using the function header () for example

However, there is a limitation - you can not send a piece of HTTP body, and then change the header, because it goes to the HTTP body

To avoid this, use constructions of the following form:

 ob_start(); $page = SomeFunctionOrClassMethod(); print $page; ob_flush(); 

Here, SomeFunctionOrClassMethod() means something that will generate this page for you. In the process of generation, a situation may arise when it will be necessary to write a header — no question, thanks to ob_start (), there has not yet been any output to the HTTP stream. The output will be only when you call ob_flush ()