title: Warning: Cannot modify header information - headers already sent by...header refresh, redirect
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8028957/headers-already-sent-by-php">Headers already sent by PHP</a>  </p>
  </blockquote>
  
  
  
  <p>I am getting this error when I try to redirect after running a user login script.</p>
  
  <p>It attempts to change the header data at this line:</p>
  
  <pre><code>header("Refresh: 2; url=index.php");
  </code></pre>
  
  <p>This is the full login.php script.</p>
  
  <pre><code>&lt;?php 
  session_start();
  require_once('init.php');
  
  $username = trim($_POST['username']);
  // create a new object
  $login = new Auth($_POST, $dbh);
  
      if($login-&gt;validateLogin()){
  
          $_SESSION['loggedin'] = true;
          $_SESSION['username'] = $username;
          $list = $login-&gt;getUserInfo();
          $_SESSION['id'] = $list['id'];
          $_SESSION['thumb'] = $list['img_thumb'];
  
          echo '&lt;div class="content"&gt;';
          echo '&lt;h1&gt;Thank you for logging in '.$username.'. Redirecting...&lt;/h1&gt;';
          echo '&lt;/div&gt;';
          header("Refresh: 2; url=index.php");
  
      }else{
  
          echo '&lt;div class="content"&gt;';
          echo '&lt;h1&gt;Sorry but we didn\'t recognise those login details, please try again.&lt;/h1&gt;';
          echo '&lt;/div&gt;';
  
      }
  
  
  
  require_once('inc/footer.inc.php');
  ?&gt;
  </code></pre>
  
  <p>The init.php file calls the header.php containing the html5 doctype, meta tags etc.</p>
  
  <p>How can I redirect the user to the index page after logging them in and send the header information as usual? </p>