PHP
  Home arrow PHP arrow Page 3 - Using HTTP Compression in PHP: Make Yo...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 5 stars5 stars5 stars5 stars5 stars / 26
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · The first part of the series goes through the practical implementation of HTTP...
       · Excellent information there. There is a small bug in your function...
       · Thank you for posting your feedback and comments, and I'm glad to know the article...
       · Removing all line-breaks will mess text inside TEXTAREA or PRE tags.In other...
       · I don't understand the point of this article. PHP does all of this work for you when...
       · Thank you for your comments. What you say is true about TEXTAREA and PRE tags....
       · Thank you for commenting on this article. With reference to your question, certainly...
       · Thank you for finding the bug and your code fix we really appreciate it. We have...
       · Has anyone developed a fix for this issue yet? I'd like to strip whitespaces from my...
       · This is a response to a message posted on Friday, 16 Jun 2006, with reference to...
     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway