Php - Problems with output compression

I had a problem trying to use php output compression, I searched for many hours and I still have no hints ...

Let's look at a simple script:

<?php $response = "abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh"; if(function_exists('ob_gzhandler')) { ob_start('ob_gzhandler'); } else { ob_start(); } echo $response; ob_end_flush(); 

This is a method that I found all over the Internet, and it worked for me ... but nothing more (and I don’t know why).

If I look at the http headers when I call this script:

Request:

 Host: 192.168.51.191 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3 Accept-Encoding: gzip, deflate Connection: keep-alive Cache-Control: max-age=0 

Answer:

 Connection: Keep-Alive Content-Type: text/html Date: Tue, 26 Jan 2016 15:19:07 GMT Keep-Alive: timeout=5, max=100 Server: Apache/2.4.9 (Win64) OpenSSL/1.0.1g PHP/5.5.12 Transfer-Encoding: chunked Vary: Accept-Encoding X-Powered-By: PHP/5.5.12 

You can see that the response is NOT encrypted (Firebird gives me the answer 0.06ko), and the server sends the answers using encoded encoding.

I tried an alternative method for sending archived responses:

 <?php $response = "abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh"; $replyBody = gzencode($response, 9, FORCE_GZIP); header("Content-Encoding: gzip"); echo $replyBody; 

And the response headers follow (the request headers are always the same):

Answer:

 Connection: Keep-Alive Content-Type: text/html Date: Tue, 26 Jan 2016 15:29:01 GMT Keep-Alive: timeout=5, max=100 Server: Apache/2.4.9 (Win64) OpenSSL/1.0.1g PHP/5.5.12 Transfer-Encoding: chunked X-Powered-By: PHP/5.5.12 

As you can see, this is basically the same behavior as in the first method.

Then if I try this:

 <?php $response = "abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh"; $replyBody = gzencode($response, 9, FORCE_GZIP); echo $replyBody; 

I get something similar to an encrypted answer (random characters), and the output size is 0.03ko.

Here are the relevant HTTP response headers:

 Connection: Keep-Alive Content-Length: 31 Content-Type: text/html Date: Tue, 26 Jan 2016 15:32:46 GMT Keep-Alive: timeout=5, max=100 Server: Apache/2.4.9 (Win64) OpenSSL/1.0.1g PHP/5.5.12 X-Powered-By: PHP/5.5.12 

This allows me to think that the zipping part is working correctly because the size of the output has been reduced (obviously, it cannot be read because the browser cannot know that this is compressed content).

What is where I am lost ...

If I understand correctly, when I manually send zipped data (using gzencode), if I set the header to "Content-Encoding: gzip", the web server / php seems to unzip it before sending it to the browser ??? How is this possible?

And why does it send it as “tagged” data instead of setting the Content-Length header?

I tried setting the Content-Length manually in response; it doesn’t change anything (it will not appear in the response headers, I will still have the answer “chunked”.

I saw somewhere that I needed to write a Content-Length header before sending other data or headers in order to avoid a “chunked” response, I tried it and still had the same results.

I thought this might be a problem with specification characters at the beginning of my php test script, but it is saved in UTF-8 without specification encoding, so I don't think this is a problem.

I have this problem on my development computer (using wampserver) and in a production environment (IIS), it previously worked on both servers.

I have this problem using multiple browsers and I checked the sizes of the answers I wrote earlier with a violinist.

Does anyone see where there might be a problem?

Thank you in advance

+5
source share

Source: https://habr.com/ru/post/1241522/


All Articles