title: How to redirect correctly using header in php?
body: <p>My code -</p>
  
  <pre><code>&lt;?php
      error_reporting(E_ALL);
  ?&gt;
  &lt;?php
      session_start();    
  
      if(isset($_POST["subLogin"]))
      {
          $uname = trim($_POST["tbUser"]);
          $pass = $_POST["pasUser"];
          if($uname == "") 
          {
              header('Location: index.php?e=1');
          }
          else if($pass == "") 
          {
              header('Location: index.php?e=2');
          }
          $unameF = "";
          $passF = "";    
  
          $userfile = fopen("DB/userDB.txt", "r");
          while(!feof($userfile))
          {    
  
              $arr1 = explode(":", fgets($userfile));
              $unameF = trim($arr1[1]);
              $arr1 = explode(":", fgets($userfile));
              $passF = trim($arr1[1]);
              $arr1 = explode(":", fgets($userfile));
              $type = trim($arr1[1]);
              if($unameF == $uname)
              {
                  $_SESSION["uname"] = $uname;
                  $_SESSION["type"] = $type;
                  header('Location: welcome.php');
              }
          }
          header("Location: index.php?e=0");
      }
      else
      {
          header('Location: index.php');
      }
  ?&gt;
  </code></pre>
  
  <p>Here, "header("Location: index.php?e=0");" always gets executed even if the username matches. And "welcome.php" is never reached. If "header("Location: index.php?e=0");" this line is commented out, then it works fine. Any solutions??</p>