2

I've come across this line in some PHP code I've inherited:

header('Content-Encoding: ', true);

Looking at the MDN docs for Content-Encoding 'true' is not a valid value. It is expecting something like gzip or deflate, not true.

I also looked and I can't see any special behaviour in PHP if you set this to true.

However in the code I am looking at this line has the following effect: it makes the file which is being output be zipped. It also adds an empty Content-Encoding header to the http response headers. The latter I understand - the former not.

There is some interaction here with a use of PHP's ob_gzhandler() which has been called and will have have already correctly set the Content-Encoding header correctly to gzip.

If I comment out this line then the file is not zipped.

What appears to be happening is this:

ob_gzhandler() sets the Content-Encoding header to gzip. Then the subsequent line: header('Content-Encoding: ', true) - which is called after ob_end_clean() somehow enables the prior setting - but without this the prior setting by ob_gzhandler is not 'activated'. Does this make sense?

Basically - why would anyone want to do:

header('Content-Encoding: ', true);?
Martin
  • 22,212
  • 11
  • 70
  • 132
  • 4
    It isn't concatenated so it isn't the value. It is the second parameter of the `header` function. `The optional replace parameter indicates whether the header should replace a previous similar header, or add a second header of the same type.` http://php.net/manual/en/function.header.php – chris85 Apr 14 '17 at 16:14
  • You are correct Chris about the replacement. Thanks. However it seems this is a special case. If you pass true and an empty value instead of a replacement I ended up with 2 Content-Encoding headers; 'Content-Encoding: gzip' and 'Content-Encoding: ' - which confused Chrome and Edge but not FireFox! –  Apr 14 '17 at 17:17
  • with `true` it replaces previous unwanted `Content-Encoding:` with empty one before output. This is not valid HTTP. – Deadooshka Apr 14 '17 at 18:02
  • I can't answer the question `Why would anyone want to do: Content-Encoding: `. What I've provided is how the PHP `header` function works. What does `curl -I http://www.example.com` bring you? – chris85 Apr 14 '17 at 18:15
  • @chris85. You solved the problem for me. Now that I know what the code does I can see that was going on. As Deadooshka says it produces invalid HTTP. Chrome and Edge choked on it. Firefox coped. As to my question why would anyone want to do 'Content-Encoding: ' it seems like a misguided attempt to do something - possibly clear the header (which you can do with header_remove()). Thanks again for putting me on the right track. –  Apr 14 '17 at 18:49

0 Answers0