0

I have trouble compressing PHP output when the Content-Length header is set. Following this very similar question of mine, it seems I've narrowed down the issue, which happens even without defining output compression in Apache's .htaccess.

So I'm using Apache 2.4.18, .htaccess is empty, AddOutputFilterByType DEFLATE text/html text/plain text/xml is commented out in Apache configuration, because I want the compression to happen in PHP.

My minimal PHP code:

<?php

ini_set("zlib.output_compression", "On");

$output = "";

for ($i = 0; $i < 4000; $i++) {
    $output .= "Foobar ";
}

// header("Content-Length: " . strlen($output));
header("Content-Type: text/html");

echo $output;

exit;

It works fine as is, the output is compressed (it's not the case if I omit the ini_set call, as expected).

Now if I uncomment the Content-Length header line, suddenly the output is not compressed anymore.

EDIT: In my case, the problem is solved, see my comments below.

sylbru
  • 1,461
  • 1
  • 11
  • 19
  • 1
    [Self-answer as a comment, because it's not really an answer] I suspect this behaviour is deliberate, as it happens on the Apache side too (see my other question, linked in this one), and considering the Content-length header is supposed to indicate the *compressed* size, not the uncompressed size as is the case here. – sylbru Dec 02 '18 at 15:10
  • Try keeping it commented out and see if the handler adds the length of compressed content. – Salman A Dec 02 '18 at 15:11
  • The reason I started experiencing this issue is because I switched to Slim using PHP-DI's Slim-Bridge (https://github.com/PHP-DI/Slim-Bridge), and the default configuration sets the `addContentLengthHeader` to `true`, forcing Slim to add the Content-Length header to the uncompressed content before handing it to Apache. The fix was as simple as adding `"settings.addContentLengthHeader" => false` in my definitions array. – sylbru Dec 02 '18 at 15:13
  • @SalmanA The line commented out, there's no Content-Length header added by anyone in the end (but the content is compressed). – sylbru Dec 02 '18 at 15:17

0 Answers0