title: HTTP2 and continuing PHP execution
body: <p>When running PHP, and you want it to immediately return HTML to the browser, close the connection (ish), and then continue processing...</p>
  
  <p>The following works when the connection is HTTP/1.1, but does not when using <code>Apache 2.4.25</code>, with <code>mod_http2</code> enabled, and you have a browser that supports HTTP/2 (e.g. Firefox 52 or Chrome 57).</p>
  
  <p>What happens is the <code>Connection: close</code> header is not sent.</p>
  
  <pre><code>&lt;?php
  
      function http_connection_close($output_html = '') {
  
          apache_setenv('no-gzip', 1); // Disable mod_gzip or mod_deflate
  
          ignore_user_abort(true);
  
          // Close session (if open)
  
          while (ob_get_level() &gt; 0) {
              $output_html = ob_get_clean() . $output_html;
          }
  
          $output_html = str_pad($output_html, 1023); // Prompt server to send packet.
          $output_html .= "\n"; // For when the client is using fgets()
  
          header('Connection: close');
          header('Content-Length: ' . strlen($output_html));
  
          echo $output_html;
  
          flush();
  
      }
  
      http_connection_close('&lt;html&gt;...&lt;/html&gt;');
  
      // Do stuff...
  
  ?&gt;
  </code></pre>
  
  <hr>
  
  <p>For similar approaches to this problem, see:</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/138374/close-a-connection-early">close a connection early</a></li>
  <li><a href="https://stackoverflow.com/questions/4806637/continue-processing-after-closing-connection">Continue processing after closing connection</a></li>
  <li><a href="https://stackoverflow.com/questions/16294106/continue-php-script-after-connection-close">Continue php script after connection close</a></li>
  </ol>
  
  <hr>
  
  <p>And as to why the <code>connection</code> header is removed, the documentation for the <code>nghttp2</code> library (as used by Apache) states:</p>
  
  <p><a href="https://github.com/nghttp2/nghttp2/blob/master/doc/programmers-guide.rst" rel="nofollow noreferrer">https://github.com/nghttp2/nghttp2/blob/master/doc/programmers-guide.rst</a></p>
  
  <pre><code>HTTP/2 prohibits connection-specific header fields. The 
  following header fields must not appear: "Connection"...
  </code></pre>
  
  <hr>
  
  <p>So if we cannot tell the browser to close the connection via this header, how do we get this to work?</p>
  
  <p>Or is there another way of telling the browser that it has everything for the HTML response, and that it shouldn't keep waiting for more data to arrive.</p>