Save 10% on our Pro WordPress plugins with discount code: 10PERCENT
Web Dev + WordPress + Security

Fast, Effective PHP Compression

PHP compression is an excellent method of conserving bandwidth and reducing client download times. We have already discussed an excellent method for CSS compression, and in this article we share a super-easy technique for compressing all PHP content without editing a single file.

Overview

Using two PHP files and two corresponding lines of .htaccess, it is possible to compress your PHP files via gzip-encoding. Browsers and other user-agents capable of interpreting gz-encoded data will employ the compressed content, while other user-agents will utilize the content uncompressed.

Step 0

Before we begin, it is important to determine if your server employs output buffering. If so, it may not be necessary to compress content via this method. Also, this PHP compression technique requires PHP version 4.0.5 or better.

Step 1

First, create two PHP files, gzip_start.php and gzip_stop. Open gzip_start.php and add this code:

<?php ob_start("ob_gzhandler"); ?>

Then, open gzip_stop.php and add this:

<?php ob_flush(); ?>

Save and upload both files to a convenient location on your server. The first file instructs the server to begin output buffering via gz-encoding. The second file instructs the server to transmit the buffered content to the user-agent, assuming it sends a header indicating its ability to process gzipped data.

Step 2

Finally, we need a way to include first file at the beginning of each PHP document and the second file at the end of each PHP document. Rather than manually adding include() or require() functions to every PHP document, we will summon the mysterious powers of .htaccess to do it all automatically. Simply add the following lines to your .htaccess file:

# dual file includes for PHP compression
php_value  auto_prepend_file  /specify/full/path/to/gzip_start.php
php_value  auto_append_file   /specify/full/path/to/gzip_stop.php

Edit the path in each line, save and upload the .htaccess file to your server. These two lines will ensure proper inclusion of both files to every PHP document subject to their influence (i.e., the containing directory and all subdirectories). The auto_prepend_file function literally prepends data, while the auto_append_file function, well, you get the idea..

Alternate Method

For an even easier PHP-compression method, simply place the following code before the (X)HTML content in any PHP script:

<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>

In this case, the ob_flush() command is unnecessary as PHP inherently flushes the buffer. The script delivers gzipped content to capable browsers and uncompressed content to incapable browsers. It’s a win-win!

Resources

Jeff Starr
About the Author
Jeff Starr = Web Developer. Book Author. Secretly Important.
USP Pro: Unlimited front-end forms for user-submitted posts and more.

16 responses to “Fast, Effective PHP Compression”

  1. Jeff Starr 2008/08/03 7:58 am

    @onealhide: Thanks for the code! :)

    @Romano: of course, it all depends on your configuration, number of files, etc., however I have not yet heard of such a thing. For an average site, I am sure you’ll be fine ;)

  2. I feel a little foolish for asking, but what should the path structure look like for the .htaccess file, does it require http:// prefix?

    # dual file includes for PHP compression php_value auto_prepend_file /specify/full/path/to/gzip_start.php php_value auto_append_file /specify/full/path/to/gzip_stop.php

    Please advise,
    thanks you,
    Steve

  3. Hi KOjunkie, you want to use the absolute path to the file. No http:// prefix required. Each path will begin with a slash and look similar in structure to the example paths used in the tutorial.

    An easy way to determine the absolute path of any file is to upload a PHP file into the same directory and place the following code within it:

    <?php echo $_SERVER['DOCUMENT_ROOT']; ?>

    ..and then call the script by navigating to the PHP file in your browser.

    Let me know if you need more assistance with this.
    Cheers,
    Jeff

  4. Shahriat Hossain 2009/04/16 7:24 pm

    Nice post dude :)

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
The Tao of WordPress: Become your own WordPress guru.
Thoughts
W3C.org has a very thorough list of accessibility tools.
The more you wake up, the more you realize you are still asleep.
7G Firewall v1.4 now available!
I would pay twice as much for a shorter/smaller/lighter phone.
Taking a much needed break in August :)
The Web was better before social media.
WP 5.8 Gutenberg/Block Widgets is breaking many sites. Fortunately Disable Gutenberg makes it easy to restore Classic Widgets with a click.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.