PHP
  Home arrow PHP arrow Page 3 - Using HTTP Compression in PHP: Make Your Web Pages Load Faster
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Using HTTP Compression in PHP: Make Your Web Pages Load Faster
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 28
    2006-04-10


    Table of Contents:
  • Using HTTP Compression in PHP: Make Your Web Pages Load Faster
  • The basics of data compression: writing a simple "crunching" PHP script
  • Moving one step forward: using real HTTP compression on parsed PHP files
  • Compressing data by "Gzip:" defining the "getCompressedContent()" function

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using HTTP Compression in PHP: Make Your Web Pages Load Faster - Moving one step forward: using real HTTP compression on parsed PHP files
    ( Page 3 of 4 )

    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.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT