Wednesday, June 15, 2011

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.

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.

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.

Now, create two PHP files, “gzip_start.php” and “gzip_stop”. Open gzip_start.php and add this:

Then, open gzip_stop.php and add this:

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.

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:

$_SERVER['HTTP_ACCEPT_ENCODING']
) && 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!

Source: perishablepress.com

http://www.whatsmyip.org/http_compression/phpgzip/

http://phamdinhlong.wordpress.com/

Add Gzip Compression Easily With PHP

What?
Gzip. Its a compression algorithm. Similar to ZIP and SIT, gzip is very common. If you are dealing with uncompressed data, you can save tons of space by gzipping your files. An image file is already compressed using jpeg or gif algorithms, and so as is the nature of data, compressing it again with gzip won't give you much. But if you have raw text, or even better, xml or html files, you are in for a treat. Most people never think of it, because image files have always been so much larger than the HTML files themselves. But more and more, HTML files are growing and growing, with more complicated page layouts and things like JavaScript, 40K HTML files are common place and easily/often surpassed. Now when you think about what HTML is, most of the HTML file itself is HTML tags, the same tags used over and over again. This makes HTML one of the BEST things to compress. You can easily compress a 40K file down to 3K.

How?
But how is it implemented? Believe it or not, nearly all browsers support gzipped content. Its been built into them for years. Its just that no one has bothered to use it. There are 3 main ways to gzip your code. Two of them involve using mod_gzip. Now mod_gzip does work, but to be honest it is a bitch to set up. Once its configured, you can set it up one of two ways. Apache can either automatically gzip all html on its way out, on the fly. Or, you can store 2 versions of each page on your site, a gzipped version, and a regular version

For example, you'd have in your home directory:
index.html -and-
index.gz
Browsers that supported gzip, would get the gz file. Ons that didn't would get the html file. Either way you set up mod_gzip, Apache will automatically send gzipped files to browsers that support it, and straight html to browsers that don't. And from the client, there is no change, no difference, the page sends, decompresses and loads exactly the same from the user point of view.

By now you might be thinking, it would be a pain in the ass to have to maintain gzipped copies of every page on my site. Your right it would, you shouldn't do it that way. You might also be thinking that having apache gzip on the fly will bog down your server. But it won't. Heres the real beauty of gzip. Because the file is so much smaller, it takes significantly less time to transmit. And the web sever doesn't have to maintain a connection for nearly as long. As a result, there is no performance hit on the server whatsoever. Add that to the fact that you are going to take a sizable bite out of your bandwidth usage each month. And the fact that high speed users are going to load your site a bit faster, and dial up users are going to load your site SIGNIFICANTLY faster. using gzip seems like a no brainer, and it surely IS a no brainer (in my opinion anyway). Wheres the downside? Configuration. Setting it up is a pain in the ass. And if you are on a web host, most don't even use mod_gzip. If you are moving your site from one server to another, you'll have to set it up on the new one again, if possible.

So I bring you the 3rd way to do it. Its so easy, you'll think your doing something wrong. Forget mod_gzip. Use PHP instead. All you have to do is put the following line at the VERY top of each PHP document, and just like magic, the entire HTML of the page will be compressed!

Code:

This is so easy, just paste it at the beginning of your PHP and thats IT! It automatically gzips the page for browsers that support it. And its portable. Nearly every webhost uses PHP, so with no configuring, you can move your site from server to server, and every server that uses PHP, you'll automatically get compressed code. If you use PHP already, you are probably thinking 'oh my god where have you been all my life' but if you don't, your probably scratching your head wondering what php is. I can't go into too much detail here, but basically: If your server is running PHP, all you have to do is change your files from webpage.html to webpages.php. And now your file is a PHP file. You can still edit it no problem in programs like Adobe GoLive. And just slip the line of code at the absolute beginning of the file, and you'll clearly see the difference.

If you are wondering about where you should use this and where you shouldn't, I'll tell you where: Everywhere. Put it at the top of every single page on your site (or use an include( ) function in PHP). Every HTML page will get at least some compression. The bigger and more complicated the page is, the better it will compress.

Now, to reiterate what I've said. If you want to gzip all your HTML code to speed up your web server, speed up your page load times, and use significantly less bandwidth, while all at the same time doing nothing different to edit, maintain, or view your web sites, then simply slip that one line of PHP at the top of each document and you are good to go!

Wondering what a gzip page really looks like? Every page on www.whatsmyip.org uses php gzipping. I've been using it for years, and it works like a charm!

How to verify its working:
gzip works so seamlessly, no doubt you'll be wondering, "is it working?" Here's the easiest way to check. Just go to the GZip Tester on this site, and type in the address of your page. Our tool will scan your page and determine if it is sending out gzipped content or not.

You can also check the "manual" way. Load the page in FireFox. Now in the 'Tools' menu, choose 'Page Info'. Now in the window, you will see a size value. That is the actual size of the html of the page as it was transmitted over the internet. Now go to 'File' and save the page to your desktop. It will save the html file and probably a folder with all the images too. Just toss the images. Now Get Info on the html file, and you will see the size of the uncompressed code. If you are not using gzip, they will be nearly identical. If you ARE using gzip, the first value will be significantly smaller than the second. If you really want to be wowed, use that method to verify this page on my site:
http://www.whatsmyip.org/htmlcharacters/
Then you will see the power of gzip!

No comments:

Router Packet Networking

Đây là video ngắn khá hay, mô tả đường đi của một gói tin trên Mạng Internet.