Header error - Warning: Can not modify header information - headers already sent

7

This is the error:

  

Warning: Can not modify header information - headers already sent by (output started at C: \ xampp \ htdocs \ newop \ template \ header.php: 80) in C: \ xampp \ htdocs \ newop \ mainindex.php on line 16

Line 80 of the header is this (it's a single line, only unpacked):

<div class="col-md-2 col-xs-2 device-select">
    <a href="budget.php?device=2">
        <img class="full-width" src="res/images/device2.jpg">
        </img>
    </a>
    <center class="popular-devices">
<?php echo $lang['header_dropdown_string_2'];?>
    </center>
</div>

... and this the code:

<?php
session_start();
if (!isset($_SESSION['user'])){ header("Location: index.php"); }
?>
<html>
  <body>
<?php
  include("template/head.php");
  include("template/header.php");
  include ("db_files/db.php");
  $usermail = mysqli_real_escape_string($db, $_SESSION['user']);
  $strSQL = "SELECT nivel FROM usuarios where email ='$usermail'";
  $query = mysqli_query($db, $strSQL);
  while ($result = mysqli_fetch_array($query)){
    if ($result['nivel'] == 1 || $result['nivel'] == 2){
    header("location: userlist:php");
  }
  
}
  $state = array(
    1 => $lang['state_name_string_1'],
    2 => $lang['state_name_string_2'],
    3 => $lang['state_name_string_3'],
    4 => $lang['state_name_string_4'],
  );
?>
  <div class="content container">
    <div class="row no-margin no-padding">
      <div class="col-md-2 hidden-sm hidden-xs">
        <div class="mainuserimage">
          <img class="full-width" src="res/images/users/user1.png"/>
        </div>
        <div>
<?php
            $strSQL = "SELECT nombre, email, telefono FROM usuarios where email ='$usermail'";
            $query = mysqli_query($db, $strSQL);
            while ($result = mysqli_fetch_array($query)){
              echo "<i class='glyphicon glyphicon-user'></i> ".$result['nombre']."<br>";
              echo "<i class='glyphicon glyphicon-envelope'></i> ".$result['email']."<br>";
              echo "<i class='glyphicon glyphicon-earphone'></i> ".$result['telefono']."<br>";
            }
?>
        </div>
      </div>
      <div class="col-md-10 col-xs-12">
      <h3 class="main-title"><?php echo $lang['mainindex_string_1']; ?></h3><br>
        <div class="row">
          <div class="col-md-2 col-xs-3">
            <p><?php echo $lang['mainindex_string_2']; ?></p>
          </div>
          <div class="col-md-2 col-xs-3">
            <p><?php echo $lang['mainindex_string_3']; ?></p>
          </div>
          <div class="col-md-2 col-xs-3">
            <p><?php echo $lang['mainindex_string_4']; ?></p>
          </div>
          <div class="col-md-2 hidden-sm hidden-xs">
            <p><?php echo $lang['mainindex_string_5']; ?></p>
          </div>
          <div class="col-md-2 col-xs-3">
            <p><?php echo $lang['mainindex_string_6']; ?></p>
          </div>
          <div class="col-md-2 hidden-sm hidden-xs">
            <p><?php echo $lang['mainindex_string_7']; ?></p>
          </div>
<?php
            $strSQL = "SELECT id, marca, modelo, imei, descripcion_problema, problem, problem2, problem2_namees, estado, fecha_creacion FROM tickets, problem2 WHERE tickets.email = '$usermail' AND tickets.problem2 = problem2.problem2_id";
            $query = mysqli_query($db, $strSQL);
            while($result = mysqli_fetch_array($query)){ ?>
              <div class="col-md-2 col-xs-3">
                <p><a href="repairdetail.php?id=<?php echo $result['id']; ?>"><i class="glyphicon glyphicon-list-alt"></i></a>  <?php echo $result['marca']; ?></p>
              </div>
              <div class="col-md-2 col-xs-3">
                <p><?php echo $result['modelo']; ?></p>
              </div>
              <div class="col-md-2 col-xs-3">
                <p><?php echo $result['imei']; ?></p>
              </div>
              <div class="col-md-2 hidden-sm hidden-xs">
                <p><?php echo $result['problem2_namees']; ?></p>
              </div>
              <div class="col-md-2 col-xs-3">
                <p><?php echo $state[$result['estado']]; ?></p>
              </div>
              <div class="col-md-2 hidden-sm hidden-xs">
                <p><?php echo $result['fecha_creacion']; ?></p>
              </div>
<?php
            }
?>
          </div>
        </div>
    </div>
  </div>
<?php
  include("template/footer.php");
?>
  </body>
</html>
    
asked by Pavlo B. 09.11.2016 в 10:42
source

2 answers

12

Move the block:

include ("db_files/db.php");
$usermail = mysqli_real_escape_string($db, $_SESSION['user']);
$strSQL = "SELECT nivel FROM usuarios where email ='$usermail'";
$query = mysqli_query($db, $strSQL);

while ($result = mysqli_fetch_array($query)){
    if ($result['nivel'] == 1 || $result['nivel'] == 2){
        header("location: userlist.php"); //<= Error: userlist:php !!!
    }
}

before:

<html>
...
  

PHP functions that send or modify HTTP headers should be   run before the requested page has been sent   to the user.

Additional information about the error: - headers already sent

A HTTP message consists of a header - Header and a body - Body that are sent in this order to the client - Client . At the moment the Body is sent, the Header can no longer be sent.

If you then try to call the function header() after HTML has been sent, you will get the famous error: Cannot modify header information - headers already sent .

Not only the header () function modifies Header but also the following functions:

Other examples which also produce error before sending the Header :

  • Spaces before opening _<?php
  • Spaces after closing ?>_
  • Use UTF-8 with BOM
  • Show / add information / content or error messages:
    HTML , echo , print , var_dump() .... etc

Examples of wrong codes:

<?php
echo "Foo Bar"; // Mostrar contenido echo
setcookie("Foo", "Bar");
 <?php // Espacio antes del "<?php"
session_start();
?>
<html> <!-- Mostrando contenido HTML -->
<head>
<?php
session_start();
?>
    
answered by 09.11.2016 / 10:46
source
3

The error occurs because you are sending the html output before modifying the header, with which, the default headers have already been sent.

You can fix it by moving the html block after the header command. Or also using the function ob_start() in the first lines of code. That will start the exit buffer cache and the headers will not be sent until the script is finished.

    
answered by 09.11.2016 в 11:31