title: Cannot modify header information - headers already sent php error
body: <p>I have made a basic messaging system with Php &amp; I'm wondering why I get this error message: </p>
  
  <p><strong>Warning: Cannot modify header information - headers already sent on line 33</strong></p>
  
  <p>Here's my full code:</p>
  
  <pre><code>    &lt;?php 
  if (isset($_GET['hash'])&amp;&amp;!empty($_GET['hash'])){
          $hash = mysqli_escape_string($con, $_GET['hash']);
          $message_query = "SELECT from_id, message FROM messages WHERE group_hash='$hash'";
          $run_messages = mysqli_query($con,$message_query);
          while($row_messages = mysqli_fetch_array($run_messages, MYSQLI_ASSOC)){
               $from_id = $row_messages['from_id'];
               $message = $row_messages['message'];
  
               $user_query = "SELECT username FROM admins WHERE id='$from_id'";
               $query_run = mysqli_query($con, $user_query);
               $run_user = mysqli_fetch_array($query_run, MYSQLI_ASSOC);
               $from_username = $run_user['username'];
  
              echo "
                  &lt;div class='widgetbox'&gt;
                      &lt;h4 class='widgettitle'&gt;&lt;i&gt;$from_username&lt;/i&gt;&lt;/h4&gt;
                      &lt;div class='widgetcontent'&gt;
                          $message
                      &lt;/div&gt;
                  &lt;/div&gt;
              ";
          }
          ?&gt;
  
          &lt;div class='widgetbox'&gt;
              &lt;form method='POST' action=''&gt;
                  &lt;?php 
                  if (isset($_POST['message'])&amp;&amp;!empty($_POST['message'])){
                      $new_message = $_POST['message'];
                      $insert_reply = "INSERT INTO messages VALUES('','$hash','$id','$new_message')";
                      $run_reply = mysqli_query($con,$insert_reply);
                      header('Location: conversations.php?hash='.$hash);
                  }
                  ?&gt;
                  &lt;h4 class='widgettitle'&gt;Enter Your Reply &lt;a class='close'&gt;&amp;times;&lt;/a&gt; &lt;a class='minimize'&gt;&amp;#8211;&lt;/a&gt;&lt;/h4&gt;
                  &lt;div class='widgetcontent'&gt;
                  &lt;textarea name='message' rows='6' cols='50'&gt;&lt;/textarea&gt;&lt;/br&gt;
                  &lt;button name='submit' type='submit' class='btn btn-primary'&gt;Send Message&lt;/button&gt;
                  &lt;/div&gt;
                  &lt;/br&gt;&lt;/br&gt;
              &lt;/form&gt;
          &lt;/div&gt;
          &lt;?php
  }else{
      header('Location: dashboard.php');
  }
  ?&gt;
  </code></pre>
  
  <p>Here's line 33:</p>
  
  <pre><code>header('Location: conversations.php?hash='.$hash);
  </code></pre>
  
  <p>I searched alot about this error message &amp; I found 2 useful answers:</p>
  
  <p>1- <code>header</code> must be at the top of your page <strong>but</strong> as you can I must place it right there because it's a part of my action scripts for my form.</p>
  
  <p>2- Using <em>Javascript</em>  header tag <strong>but</strong> that's not a good idea cause I need to process this link via <em>Php</em>: <code>conversations.php?hash='.$hash</code></p>
  
  <p>Therefore I have to fix this header problem. Please if you know how can I be do that , let me know ... I really appreciate! Thanks :)</p>