W
W
whiteleaf2014-04-24 05:16:44
PHP
whiteleaf, 2014-04-24 05:16:44

How to properly structure a document to avoid "Cannot modify header information - headers already sent by"?

Hello everyone,
I'm sorry for so many childish questions, but I'm just starting to learn programming and I ran into authorization, namely header(). I know that headers should precede all outputs, but would it be better to structure the structure to avoid them then?
I will give the sources below:
index.php

<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Blog</title>
  <link rel="stylesheet" href="css/style.css">
</head>
<body>
  <div class="container">
    <div class="top"><?php (isset($_POST['session_id'])) ? include_once "logout.php" : include_once "lgn.php"; ?></div>
    <div class="center">
      <div class="left">left</div>
      <div class="content"><?php include_once "blog.php"; ?></div>
    </div>
  </div>
</body>
</html>

lgn.php
<?php 
    $login 		= htmlentities(trim($_POST['login']));
    $password 	= md5(trim($_POST['password']));
      if (isset($_POST['submit'])) {
        include_once "db_connect.php";
        include_once "config.php";
        if (isset($login,$password)) {
        $query = "SELECT * FROM users WHERE login="."'".$login."'";
        $result = mysqli_query($con,$query);
        $user = array();
        while ($row = mysqli_fetch_array($result)) {
          foreach ($row as $key => $value) {
            if (!is_numeric($key)) {
              $user[$key] = $value;
            }
          }
        }
        if (($login === $user['login']) && ($password === $user['password'])) {
          setcookie('session_id', $user['id']);
          setcookie('login', $user['login']);
        }else{
          echo "Such user doen't exist or login/password dosn't match. Please, check them twice and try again!";
        }
      }
    }
 	?>
<div class="login">
  <form action="" method="POST">
    <table>
      <tr>
        <td><input type="text" name="login" placeholder="Login" value="<?php echo $_POST['login'] ?>"></td>
        <td><input type="password" name="password" placeholder="Password"></td>
        <td><input type="submit" name="submit"></td>
      </tr>
    </table>
  </form>			
</div>

logout.php
<table>
  <tr>
    <td><?php echo "Welcome back, ".$_COOKIE['login']; ?></td>
    <td><a href="<?php setcookie('session_id', ""); ?>">Log out</a></td>
  </tr>
</table>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LIAL, 2014-04-24
@whiteleaf

index.php you actually have an html template. organize it like this:

<?php
 if (проверка данных выводить шаблон или заглолвки отправлять) {
    что-то можно сделать тут еще. но только не echo,print или вывод какой либо
     тут можете посылать ваши хидеры (session, cookie И так далее)
  else {
  тут выводите ваш html типа echo '<html>........</html>';
}
?>

Also check the encoding of the file just in case. at one time I suffered for a very long time with such an error when the file was saved as utf8 with BOM, it is necessary WITHOUT BOM. Because this bom adds the output of some service symbols at the beginning of the file, which leads to the fact that before sending the headers to the output, data was already sent
ps: greatly simplified, but the essence should be clear

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question