[ACCEPTED]-PHP Manual GZip Encoding-compression

Accepted answer
Score: 16

Well, I think it's because you're trying 27 to compress an empty string.

I took your 26 script as you gave it, and ran it in FF 25 and IE.

Both failed, and FF said that there 24 was an issue (like you described).

However, I 23 then noticed $data is an empty string.

When 22 I set $data = "Some test data."; at the top of the file it worked 21 immediately (browser displayed "Some 20 test data."), and checking in Firebug, I 19 can see the correct headers.

Content-Encoding    gzip
Content-Length  68
Vary    Accept-Encoding
Content-Type    text/html

Edit: Also, just 18 to point out, your if ($supportsGzip) { is a bit odd, because 17 your else condition should actually echo 16 out $data, not $content.

Edit: Okay, based on your revised 15 function above, there are two key problems.

The 14 primary problem has to do with the fact 13 that you're wiping out your headers by calling 12 ob_end_clean(). A comment on the PHP Docs states that "ob_end_clean() does 11 discard headers".

This means any headers 10 you set before calling ob_end_clean() will get wiped. Also, your 9 revised function doesn't send a gzip encoding 8 header, either.

I must say that there is 7 probably no need to even use ob_start and 6 related functions here, either. Try the 5 following:

function _compress( $data ) {

    $supportsGzip = strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false;


    if ( $supportsGzip ) {
        $content = gzencode( trim( preg_replace( '/\s+/', ' ', $data ) ), 9);
        header('Content-Encoding: gzip');
    } else {
        $content = $data;
    }

    $offset = 60 * 60;
    $expire = "expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";

    header("content-type: text/html; charset: UTF-8");
    header("cache-control: must-revalidate");
    header( $expire );
    header( 'Content-Length: ' . strlen( $content ) );
    header('Vary: Accept-Encoding');

    echo $content;

}

_compress( "Some test data" );

This works in IE and FF, but I 4 didn't have time to test other browsers.

If 3 you really must use ob_start and related 2 functions, make sure you set your headers 1 after you call ob_end_clean().

Score: 7

I'd suggest to use http://php.net/manual/de/function.ob-gzhandler.php, this works out of the 2 box for me:

In my index.php I just place 1 this before some output:

    /**
     * Enable GZIP-Compression for Browser that support it.
     */
    ob_start("ob_gzhandler");

And it encodes it!

Score: 0

A few things:

  1. You probably want to add another 11 header: header('Content-Encoding: gzip');

  2. You 10 are using ob_end_clean, which deletes all 9 echoed/printed content without sending it 8 to the browser. Depending on what you're 7 trying to do, you may want to use ob_flush 6 instead.

  3. To make sure your output is buffered 5 and handled (and compressed if you use PHP's 4 output buffering compression), make sure 3 all echo/print statements are placed BETWEEN 2 the ob_start and ob_flush sttements.

--and 1 then try it again :)

More Related questions