PHP
  Home arrow PHP arrow Page 2 - An Object-based Approach to HTTP Compr...
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

An Object-based Approach to HTTP Compression in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2006-04-17

    Table of Contents:
  • An Object-based Approach to HTTP Compression in PHP
  • Object-based "Gzip" compression: creating a data compressor PHP class
  • Setting up a concrete example: putting the "DataCompressor" class to work
  • Explaining the example

  • 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


    An Object-based Approach to HTTP Compression in PHP - Object-based "Gzip" compression: creating a data compressor PHP class


    (Page 2 of 4 )

    Before I make a data compressor class that internally uses "Gzip" encoding for compressing the data passed as input argument, first let me list the "getCompressedContent()" function that I wrote in the previous tutorial, to refresh your memory of how this function looked. Here is its signature:

    function getCompressedContent($data){
        // check if browser supports gzip encoding
        if(strstr($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')){
            // start output buffer
            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 content & compress data with gzip
            $data=gzencode(preg_replace("/
    (rn|n)/","",ob_get_contents()),9);
            echo 'Hello';
            // clean up output buffer
            ob_end_clean();
            // send http header
            header('Content-Encoding: gzip');
        }
        // return data
        return $data;
    }

    Now, I hope the above function is pretty familiar to you, because I'm going to use it as the foundation for building the data compressor class that I mentioned before. Particularly, I'll define this class for use in PHP 5, but you can easily adapt its source code to include it within your PHP 4 scripts. Here's how the "DataCompressor" class looks:

    // define 'DataCompressor' class
    class DataCompressor{
        private $data;
        public function __construct($data){
            if(!file_exists($data)||!is_string($data)){
                throw new Exception('Invalid input data');
            }
            $this->data=$data;
        }
        public function fetchCompressedData(){
            // check if browser supports gzip encoding
            if(strstr($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')){
                // start output buffer
                ob_start();
                // check to see if $data is a file
                if(file_exists($this->data)){
                    // include file into output buffer
                    include($this->data);
                }
                else{
                    // echo string to output buffer
                    echo $this->data;
                }
                // crunch content & compress data with gzip
                $this->data=gzencode(preg_replace("/
    (rn|n)/",'',ob_get_contents()),9);
                // clean up output buffer
                ob_end_clean();
                // return data
                return $this->data;
            }
            return false;
        }
        public function sendEncodingHeader(){
            header('Content-Encoding: gzip');
        }
    }

    As I said earlier, I wrote the above class inspired by the source code of the previous "getCompressedContent()" function, something that should be quite obvious. As you can see, the workhorse of the class is the "fetchCompressedData()" method, which is very similar to its procedural counterpart. In this case, this method opens up an output buffer, then includes the contents of the $this->data property, and finally returns the "gzip-encoded" data, after cleaning up the respective buffer.

    Additionally, the class constructor accepts the mentioned $data incoming argument, which is properly checked inside this method to make sure it's either a file or a simple string. Any other type of data passed on to the constructor will throw an exception, which eventually will be caught by client code.

    Finally, I decided to define separately the "sendEncodingHeader()" method, providing the class with the ability to send this HTTP header when the method is specifically called. Definitely, breaking down the class code into different specific methods helps keep the whole class as a much more flexible programming structure.

    Right, now that I've shown you the signature of the "DataCompressor" class, you'll probably want to see how it can be used within a basic sample PHP script. For this reason, jump into the next section, so you can learn how this class is utilized as part of a pretty useful hands-on example.

    More PHP Articles
    More By Alejandro Gervasio


       · In this second tutorial of the series, HTTP compression is implemented by using a...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





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