title: Force download with PHP then redirect
body: <p>I know this question has been asked many times before but I can't find an answer to suit my needs.</p>
  
  <p>I need to find a way to force the download of a file and then, after the download has started, redirect to a "thanks for downloading" page.</p>
  
  <p>So far I have:</p>
  
  <pre><code>&lt;?php
  ob_start();
  
  $token = $_POST['validationCode'];
  
  if(isset($token)){
  
      $connect = mysql_connect('localhost', 'root', 'root');
      $db = mysql_select_db('mydb');
  
      if (!$connect || !$db){
          die('Connect Error (' . mysql_connect_errno() . ') '
                  . mysql_connect_error());
      }
  
      $sql = mysql_query("SELECT * FROM emailaddresses WHERE token='$token'");
      $result = mysql_fetch_array($sql);
      if($result){
          header('Location: complete.php');
          header('Content-type: application/mp3');
          header('Content-Disposition: attachment; filename=track.mp3');
          $f = file_get_contents('downloads/track.mp3');
          print $f;
          $sql = "UPDATE emailaddresses SET download=1 WHERE token='$token'";
          $result = mysql_query($sql);
      }
      else{
          echo "There was a problem downloading the file" . mysql_error();
      }
  }
  
  ob_end_flush();
  
  ?&gt;
  </code></pre>
  
  <p>It's important to hide the download file's location otherwise I would have just created an HTML link to the file.</p>
  
  <p>I obviously can't put a redirect header below the other headers as it just won't work.  I can't really see where to go from here apart from opening this in a pop-up and directing the main window to the "thank you" page - but that is a LAST resort.</p>
  
  <p>Can anyone give any suggestions?</p>
  
  <p>Cheers,</p>
  
  <p>Rich</p>