title: Send only part of headers (preload / prefetch)
body: <p>Our PHP-system generates webpages like this:</p>
  
  <pre><code>1. [Init]
  2. [Do some DB-queries and calculations, might take some seconds, might end up in sending a 301 or 404]
  3. [Output results]
  </code></pre>
  
  <p>Now I want to speed up the pages by preloading css, fonts and js-files. It would be ideal if I could send headers like this:</p>
  
  <pre><code>1. [Init]
  header("Link: &lt;/style.css&gt;; rel=preload; as=style", false);
  2. [Do some DB-queries and calculations, might take some seconds, might end up in sending a 301 or 404]
  3. [Output results]
  </code></pre>
  
  <p>but the preload-header is sent only when output started in step 3 - so there is not much gain in speed. So I tried:</p>
  
  <pre><code>1. [Init]
  header("Link: &lt;/style.css&gt;; rel=preload; as=style", false);
  flush();
  2. [Do some DB-queries and calculations, might take some seconds, might end up in sending a 301 or 404]
  3. [Output results]
  </code></pre>
  
  <p>Wow, this really speeds things up! But well, sending a 301 or 404 header in step 2 ends up in the message:</p>
  
  <p><strong>Warning</strong>: Cannot modify header information - headers already sent by ...</p>
  
  <p>So I need to put it there:</p>
  
  <pre><code>1. [Init]
  2. [Do some DB-queries and calculations, might take some seconds, might end up in sending a 301 or 404]
  header("Link: &lt;/style.css&gt;; rel=preload; as=style", false);
  3. [Output results]
  </code></pre>
  
  <p>which doesn't bring a big advantage in speed, because the big time-consuming things are done in step 2.</p>
  
  <p>Q: Since headers are transferred line by line from the webserver to the browser I was wondering if there is a way to send the preload-header to the browser and a 301-header some seconds afterwards like:</p>
  
  <pre><code>1. [Init]
  header("Link: &lt;/style.css&gt;; rel=preload; as=style", false);
  send_part_of_headers();
  2. [Do some DB-queries and calculations, might take some seconds, might end up in sending a 301 or 404]
  3. [Output results]
  </code></pre>