HomePHP Page 3 - Using HTTP Compression in PHP: Make Your Web Pages Load Faster
Moving one step forward: using real HTTP compression on parsed PHP files - PHP
Web developers are always looking for ways to reduce the loading time of their pages. This article, the first of three parts, shows you how to make PHP pages load faster by showing you how to compress dynamic PHP pages. Techniques covered include using PHP's built-in “gzencode()” function, along with output buffering control functions.
Once I demonstrated how to eliminate some redundant data from previously parsed PHP files, I could go one step forward and write a simple PHP function, based on the script that you saw in the previous section. This function looks like this:
function getCompressedData($data){ ob_start(); // check to see if $data is a file if(file_exists($data)){ // include file into output buffer include($data); } else{ // echo string to output buffer echo $data; } // crunch buffer data $data=preg_replace("/(rn|n)/","",ob_get_contents()); // clean up output buffer ob_end_clean(); // return data return $data; }
And eventually the above function could be called as follows:
// display crunched data echo getCompressedData('sample_file.php');
As shown above, the "getCompressedData()" function implements the same logic for reducing the size of input data, and additionally includes the capability to determine whether the corresponding incoming argument is a file or not. In either case, data is crunched by using the same technique that I discussed before, and finally is echoed to the browser.
So far, the above approach is merely a rough method for optimizing parsed PHP files. The best part is that the prior sample code might be easily modified, in order to apply "Gzip" HTTP compression on the output generated by the previous "sample_file.php" file. Considering this useful concept, a basic script that compresses a specific parsed PHP file, could be coded like this:
// start output buffer ob_start(); // include file contents include('sample_file.php'); $contents=ob_get_contents(); // close output buffer ob_end_clean(); // compress data by gzip $contents=gzencode($contents,9); // send http header header('Content-Encoding: gzip'); // display data echo $contents;
Although the above script maintains the same general structure that you saw before, in fact it exposes a few remarkable changes that make it work very differently. First of all, the script begins opening a new output buffer and parses the corresponding "sample_file.php" file. Then, the respective output is fetched and stored in the $contents variable, and finally the buffer is cleaned up by the "ob_end_clean()" function.
As you may have guessed, the next few lines of the script perform true "Gzip" compression on the data by using the PHP built-in "gzencode()" function (the second argument, in this case "9", means the highest level of compression) and by sending the appropriate HTTP header to the browser, in order to indicate that data will be compressed and then transmitted in turn. On the client side, the browser should decompress the data back to its original state and display the contents. Simple and sweet.
At this point, I hope you learned how "Gzip" compression can be used to reduce the amount of data transmitted from the Web server to the client, after a given PHP file has been parsed. Reducing the amount of data transmitted, of course, reduces the download time. Therefore, I'd like to demonstrate how to use the functionality of a "data crunching" script, together with the natural ability of PHP to compress parsed files, in order to define a unique function that combines all of these advantages. Want to see how this function looks? Go ahead and read the next section.