0

I want to use gzip for my mobile version of site. I tried to do it adding following line at the top of my php file

ob_start("ob_gzhandler");

But it gives me following error. enter image description here

I've been searching and trying number of ways but nothing could compress the page. How to achieve this?

KutePHP
  • 2,206
  • 6
  • 36
  • 54
  • Is ob_gzhandler installed in your PHP runtime? And does your browser accept gzipped content? You can check it with `$_SERVER['HTTP_ACCEPT_ENCODING']` – TiMESPLiNTER Oct 31 '13 at 09:36
  • yes to both your questions – KutePHP Oct 31 '13 at 09:40
  • 1
    Propably you send unzipped content before you call `ob_start("ob_gzhandler");`? Mixed zipped/unzipped content can issue this error. Also have a look here: http://stackoverflow.com/a/6403260/1652031 – TiMESPLiNTER Oct 31 '13 at 09:41
  • Try using different browsers to make sure it works on other. – The Alpha Oct 31 '13 at 09:48
  • [Check this answer](http://stackoverflow.com/questions/5441784/why-does-ob-startob-gzhandler-break-this-website) and [this post](http://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/), could be helpful. – The Alpha Oct 31 '13 at 09:53
  • @TiMESPLiNTER - hey dear! thanks for drawing my attention to this. It works now :) – KutePHP Oct 31 '13 at 09:56

3 Answers3

3

Maybe you had the same problem as me and actually have an utf-8 file WITH an UTF-8 BOM inside *.
I think gzip in combination with UTF-8 BOM gives an encoding problem.

Notes:

  • Not all editors are able to show if the BOM is there or not. I had to actually use another editor, Notepad++, to realize there was a BOM there and remove it there via "Convert to utf-8 without BOM" and then saving the file. (Also closing it in my original editor first.) But could as well be that you can set your editor not the include the BOM.
  • Possibly this only occurs when php error reporting is on

* More about UTF BOM:

Community
  • 1
  • 1
e-motiv
  • 5,795
  • 5
  • 27
  • 28
  • Perfect. I also use Notepad++. Change to **Encoding in UTF-8 without BOM** make the code `ob_start("ob_gzhandler");` works with no more error. – eQ19 Apr 16 '15 at 14:17
  • Had similar problems with BOM using Eclipse. You can not see it and have to remove it with another editor but you can check if BOM is inserted - go to File->Properties and see if BOM is mentioned under 'Text file encoding' box – besimple Apr 22 '15 at 22:32
1

May be you are using Apache's gzip compression that compress js/css files, again use of ob_start('ob_gzhandler') will compress that compression and browser will not handle that.

check here may be it will help you.

Community
  • 1
  • 1
Harish Singh
  • 3,359
  • 5
  • 24
  • 39
-3

simply use double quotes as "ob_start('ob_gzhandler')"; to avoid content encoding error

Szymon
  • 42,577
  • 16
  • 96
  • 114