Home arrow PHP arrow 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.

  1. Using HTTP Compression in PHP: Make Your Web Pages Load Faster
  2. The basics of data compression: writing a simple "crunching" PHP script
  3. Moving one step forward: using real HTTP compression on parsed PHP files
  4. Compressing data by "Gzip:" defining the "getCompressedContent()" function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 32
April 10, 2006

print this article



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){
    // check to see if $data is a file
        // include file into output buffer
        // echo string to output buffer
        echo $data;
    // crunch buffer data
    // clean up output buffer
    // 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
// include file contents
// close output buffer
// compress data by gzip
// 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.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: