Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
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

About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
BBQ Pro: The fastest firewall to protect your WordPress.

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 »
Wizard’s SQL for WordPress: Over 300+ recipes! Check the Demo »
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.