title: redirect after download pdf file using a form
body: <p>I know the problem of "headers already sent" when trying to redirect after readfile. My question is how to handle such a situation so I get the website focus back or redirect to other website.
  I've created a simple example of what I'm dealing with. I've created a form and after the user gives a name and an email address, he can download a file, but after I can't do other things or redirect to other url. I can't find a way and think of a solution how should I rewrite my script and create things in proper way. How these download forms should be build in a right way?   </p>
  
  <pre><code>&lt;body&gt;
  &lt;?php
  
  $errors = [];
  $name = "";
  $email = "";
  
  if (isset($_POST["submit"])) {
      $name = htmlspecialchars(stripslashes(trim($_POST["name"])));
      $email = htmlspecialchars(stripslashes(trim($_POST["email"])));
  
      if(!preg_match("/^[A-Za-z .'-]+$/", $name)){
        $errors[] = "Invalid name";
      }     
      if(!preg_match("/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/", $email)){
        $errors[] = "Invalid email";
      }
  
              if (empty($errors)) {                           
  
                      $file = "files/live.pdf";
  
                      header('Content-Description: File Transfer');
                      header('Content-Type: application/pdf');
                      header('Content-Disposition: attachment; filename=' . basename($file));
                      header('Content-Transfer-Encoding: binary');
                      header('Expires: 0');
                      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                      header('Pragma: public');
                      header('Content-Length: ' . filesize($file));
                      ob_clean();
                      flush();                
                      readfile($file);
  
                      echo "ok"; 
                      header("Location: http://something.com");
              }
  
          }
  ?&gt;  
  
  &lt;form method="POST"&gt;
      &lt;label for="name"&gt;Name&lt;/label&gt;
      &lt;input type="text" id="name" name="name" value="&lt;?= $name ?&gt;" required&gt;&lt;br&gt;
      &lt;label for="email"&gt;Email&lt;/label&gt;
      &lt;input type="email" id="email" name="email" value="&lt;?= $email ?&gt;" required&gt;&lt;br&gt;
      &lt;button type="submit" name="submit"&gt;Download&lt;/button&gt;&lt;br&gt;
  &lt;/form&gt;
  
  &lt;p&gt;
      &lt;?php if (!empty($errors)) : ?&gt;
              &lt;ul&gt;
                      &lt;?php foreach ($errors as $error) : ?&gt;
                              &lt;li&gt;&lt;?= $error ?&gt;&lt;/li&gt;
                      &lt;?php endforeach; ?&gt;
              &lt;/ul&gt;
      &lt;?php endif; ?&gt;
  &lt;/p&gt;            
  
  &lt;/body&gt;
  </code></pre>