PHP ob_gzhandler extra characters

179 views Asked by At

I have a blended website where we have an ExpressionEngine CMS with a Magento store. In our EE site, I have a template where I call a plugin to retrieve the store cart info. We use Magento's authentication so the logged in user is the Magento user.

We want to show something like this:

first

The code in the plugin connects to Magento, gets the loggedin user, if they are logged in and sets the number of cart items.

This is the HTML that the plugin is supposed to return to the template:

<span class="carticon">(0)</span><a href="http://www.example.com/store/checkout/cart/">My Cart</a><span>Welcome, MB34!</span><a href="http://www.example.com/store/customer/account/logout/">Logout</a>

But, if I enable gZip on ExpressionEngine, I get the ERR_CONTENT_DECODING_FAILED exception because Magento doesn't have built-in gZip. Currently we do not have mod_deflate enabled so how would EE be able to gZip? It has to be through the ob_gzhandler.

Now, if I modify my plugin to use ob_gzhandler like this:

ob_start("ob_gzhandler");
echo trim($result);
ob_end_flush();

I get extra characters at the end of the output:

second

Any ideas what is causing this and how to fix it?

We will be enabling mod_deflate soon; will that fix it? I mean I won't have to use the ob_gzhandler then, right?

2

There are 2 answers

1
Havenard On BEST ANSWER

When using ob_gzhandler its important that you don't mix content with other encodings. By forcing ob_end_flush() you may be causing this buffer to terminate before you are actually done sending content. A simple empty line by the end of the file may be causing this. I suggest you skip ob_end_flush() and let the buffer end naturally when your PHP script terminates.

0
eQ19 On

This code is work for me when using ob_get_contents

ob_start("ob_gzhandler"); 
echo trim($result); 
$gzcontent = ob_get_contents(); 
ob_end_clean();