PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Speeding Up Your PHP Web Site

GZipping Your Content

While tools such as YUI Compressor and htmlcompressor reduce the size of files in their original form, we can also compress files across the wire using gzip encoding.

You can gzip encode all types of files, including HTML, CSS, JavaScript and images.

There are two ways to implementing gzipping of content:

  • Allow Apache to automatically gzip all content. This can even intercept output from a PHP script before it's sent back to the user.
  • Use PHP's output buffering to manually gzip output. You can use gzip functions in PHP for existing files (such as images), but we'll only look at output buffering in this article.

If possible, you should leverage the tools Apache provides, since this is far simpler, but if you don't use Apache then you might need to look at implementing an in-code solution.

How Gzipping Works

When a user visits a web site, one of the headers their browser may send is Accept-Encoding. This header tells the web server whether or not the browser will accept gzip-encoded data.

If the browser doesn't indicate it will accept encoded data, you simply send back the original data, however if they will accept encoded data you should send it where possible.

Tip: It is possible that a user's browser may not correctly ask for gzip-encoding. This can often occur to anti-virus software they have installed. You can send a gzipped JavaScript file which sets a cookie: if their browser does in fact suppport gzipped content the cookie will be set and you can then always send them their data gzipped.

Gzipping Content With Apache

Using Apache to automatically gzip content is by far the easiest way to get started. This is achieved using the mod_deflate module. You can read all about this module at http://httpd.apache.org/docs/2.0/mod/mod_deflate.html.

If you add a directive such as AddOutputFilterByType DEFLATE text/html text/plain text/xml to your configuration, all content with the specified mime types will be gzipped automatically if the browser will accept it.

The documentation for mod_deflate includes more complicated examples which will result in more content being gzipped.

Gzipping Content With PHP

It is slightly more complicated to gzip content when using PHP. The basic algorithm for doing so is as follows:

  1. Check if user accepts gzip-encoded data
  2. If not, send back page as usual
  3. Otherwise, send a Content-Encoding header
  4. Compress all the content and send it back

Compressing the content is relatively easy using the gzencode() function. This function accepts the data to encode as the first argument and the compression level (0-9) as the second argument. The higher the compression level the smaller the output will be (but the longer it will take to compress).

The following listing demonstrates how to actually implement this in your own web site.

Listing 2 Gzipping content using output buffering and gzencode() (listing-2.php)
<?php
    $supportsGzip = strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false;
 
    ob_start();
    echo "<html>....</html>";
    $content = ob_get_clean();
 
    header('Content-Type: text/html');
 
    if ($supportsGzip) {
        header('Content-Encoding: gzip');
        echo gzencode($content, 9);
    }
    else {
        echo $content;
    }
?>

In This Article