Question

Gzip

Asked by: cool888

Hi all,

I need to compress my forum using Gzip, how can I do that?
by putting this enough? ob_start ("ob_gzhandler");

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2003-10-09 at 23:30:37ID20762683
Tags

gzip

,

needs

,

ob_gzhandler

Topic

PHP Scripting Language

Participating Experts
3
Points
50
Comments
10

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. mod_gzip compression
    Hi, this may sound petty, but I have an 11K HTML file located on a server residing in USA. My goal is to allow users in New Zealand to be able to see this page in the minimal time frame on their browser. Should I implement html compression (basically cleaning out the usel...
  2. Compress in GZIP
    I have this function to Unzip Gzip compressed files. What I need is to be able to compress files in the Gzip format. Using this function as an example, how could I rewrite it to compress in to a "*.gz" file? Thanks in advance. /** * Unzip a gzipped (.gz) f...
  3. GZIP Compression
    Hi I wrote some code to zip xml that i am sending from servlet to browser. when i am testing this code in firefox everything working but ie7 i can't see nothing. my code is : OutputStream out = null; String encoding = request.getHeader("Accept-Encoding"...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: MarcosBLPosted on 2003-10-10 at 09:19:50ID: 9528724

Just use this class... my first response, don't be rude, if you need help just ask. Code © from Leknor


<?php

// News: I had once said that when PHP4.0.5 comes out I will reccomend the built in
// ob_gzhandler over my code unless you are generating flash or images on the fly.
//
// I was wrong. PHP4.0.5 is out and ob_gzhandler doesn't work for me.

// Note: This is rather cool: http://Leknor.com/code/gziped.php
// It will calculate the effects of this class on a page.
// compression level, cpu time, download time, etc

// Note: this may better for some sites:
// http://www.remotecommunications.com/apache/mod_gzip/
// I've read that the above doesn't work with php output.

class gzip_encode {
    /*
     * gzip_encode - a class to gzip encode php output
     *
     * By Sandy McArthur, Jr. <Leknor@Leknor.com>
     *
     * Copyright 2001 (c) All Rights Reserved, All Responsibility Yours.
     *
     * This code is released under the GNU LGPL Go read it over here:
     * http://www.gnu.org/copyleft/lesser.html
     *
     * I do make one optional request, I would like an account on or a
     * copy of where this code is used. If that is not possible then
     * an email would be cool.
     *
     * How to use:
     * 1. Output buffering has to be turned on. You can do this with ob_start()
     *    <http://php.net/manual/function.ob-start.php> or in the php config
     *    file. Nothing bad happens if output buffering isn't turned on, your
     *    page just won't get compressed.
     * 2. Include the class file.
     * 3. At the _very_ end of your script create an instance of the encode
     *    class.
     *
     * eg:
     *    ------------Start of file----------
     *    |<?php
     *    | ob_start();
     *    | include('class.gzip_encode.php');
     *    |?>
     *    |<HTML>
     *    |... the page ...
     *    |</HTML>
     *    |<?php
     *    | new gzip_encode();
     *    |?>
     *    -------------End of file-----------
     *
     * Things to note:
     * 1. There is no space before the beginning of the file and the '<?php' tag
     * 2. The ob_start() line is optional if output buffering is turned on in
     *    the main config file.
     * 3. Turning on and off output buffering just won't work.
     * 4. There must be nothing after the last '?>' tag at the end of the file.
     *    Be careful of a space hiding there.
     * 5. There are better ways to compress served content but I think this is
     *    the only way to compress php output.
     * 6. Your auto_prepend_file is a good place for the ob_start() and
     *    your auto_append_file is a good place for new gzip_encode().
     * 7. If you put new gzip_encode() in your auto.append file then you can
     *    call ob_end_flush() in your script to disable compression.
     *
     * This was written from scratch from info freely available on the web.
     *
     * These site(s) were useful to me:
     *    http://www.php.net/manual/
     *    http://www.ietf.org/rfc/rfc2616.txt (Sections: 3.5, 14.3, 14.11)
     *
     * Requirments:
     *    PHP 4.0.1+:    I use the '===' operator, and output buffering, crc32();
     *    zlib:        Needed for the gzip encoding. (Odds are you have it)
     *
     * Benchmarks:
     *    Take a look at http://Leknor.com/code/gziped.php and feed it a page to
     *    get an idea of how it will preform on your data or page.
     *
     * To Do:
     * 1. I have reports of no content errors. I can't seem to duplicate this.
     *    Please visit my discussion boards if you think you may be able to help
     * 2. The Accept-Encoding isn't handled to spec. Check out 14.3 in RFC 2616
     *    to see how it should be done.
     *
     * Change Log:
     *    0.67:    Added Vary header to aid in caching.
     *    0.66:    Big bug fix. It wouldn't compress when it should.
     *    0.65:    Fix for PHP-4.0.5 suddenly removing the connection_timeout() function.
     *    0.62:    Fixed a typo
     *    0.61:    Detect file types more like described in the magic number files, also
     *        added detection for gzip and pk zip files.
     *    0.6:    Detect common file types that shouldn't be compressed, mainly
     *        for images and swf (Shockwave Flash doesn't really accept gzip)
     *    0.53:    Made gzip_accepted() method so everyone can detect if a page
     *        will be gzip'ed with ease.
     *    0.52:    Detection and graceful handling of improper install/missing libs
     *    0.51:    Added FreeBSD load average detection.
     *    0.5:    Passing true as the first parameter will try to calculate the
     *        compression level from the server's load average. Passing true
     *        as the second parameter will turn on debugging.
     *    0.4:    No longer uses a temp file to compress the output. Should speed
     *        thing up a bit and reduce wear on your hard disk. Also test if
     *        the http headers have been sent.
     *    0.31:    Made a small change to the tempnam() line to hopefully be more
     *        portable.
     *    0.3:    Added code for the 'x-gzip'. This is untested, I don't know of
     *        any browser that uses it but the RFC said to look out for it.
     *    0.2:    Checks for 'gzip' in the Accept-Encoding header
     *    0.1:    First working version.
     *
     * Thanks To (Suggestions and stuff):
     *    ?@boas.anthro.mnsu.edu    http://php.net/manual/function.gzcompress.php
     *    Kaoslord        <kaoslord@chaos-productions.com>
     *    Michael R. Gile        <gilem@wsg.net>
     *    Christian Hamm        <chh@admaster.de>
     *    Anil Madhavapeddy    <anil@recoil.org>
     *
     * The most recent version is available at:
     *    http://Leknor.com/code/
     *
     */

    var $_version = 0.67; // Version of the gzip_encode class

    var $level;        // Compression level
    var $encoding;    // Encoding type
    var $crc;        // crc of the output
    var $size;        // size of the uncompressed content
    var $gzsize;    // size of the compressed content

    /*
     * gzip_encode constructor - gzip encodes the current output buffer
     * if the browser supports it.
     *
     * Note: all arguments are optionial.
     *
     * You can specify one of the following for the first argument:
     *    0:    No compression
     *    1:    Min compression
     *    ...    Some compression (integer from 1 to 9)
     *    9:    Max compression
     *    true:    Determin the compression level from the system load. The
     *        higher the load the less the compression.
     *
     * You can specify one of the following for the second argument:
     *    true:    Don't actully output the compressed form but run as if it
     *        had. Used for debugging.
     */
    function gzip_encode($level = 3, $debug = false) {
    global $HTTP_ACCEPT_ENCODING;
    if (!function_exists('gzcompress')) {
        trigger_error('gzcompress not found, ' .
            'zlib needs to be installed for gzip_encode',
            E_USER_WARNING);
        return;
    }
    if (!function_exists('crc32')) {
        trigger_error('crc32() not found, ' .
            'PHP >= 4.0.1 needed for gzip_encode', E_USER_WARNING);
        return;
    }
    if (headers_sent()) return;
    if (connection_status() !== 0) return;
    $encoding = $this->gzip_accepted();
    if (!$encoding) return;
    $this->encoding = $encoding;

    if ($level === true) {
        $level = $this->get_complevel();
    }
    $this->level = $level;

    $contents = ob_get_contents();
    if ($contents === false) return;

    $gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00"; // gzip header
    $size = strlen($contents);
    $crc = crc32($contents);
    $gzdata .= gzcompress($contents, $level);
    $gzdata = substr($gzdata, 0, strlen($gzdata) - 4); // fix crc bug
    $gzdata .= pack("V",$crc) . pack("V", $size);

    $this->size = $size;
    $this->crc = $crc;
    $this->gzsize = strlen($gzdata);

    if ($debug) {
        return;
    }

    ob_end_clean();
    Header('Content-Encoding: ' . $encoding);
    Header('Vary: Accept-Encoding');
    Header('Content-Length: ' . strlen($gzdata));
    Header('X-Content-Encoded-By: class.gzip_encode '.$this->_version);

    echo $gzdata;
    }
   

    /*
     * gzip_accepted() - Test headers for Accept-Encoding: gzip
     *
     * Returns: if proper headers aren't found: false
     *          if proper headers are found: 'gzip' or 'x-gzip'
     *
     * Tip: using this function you can test if the class will gzip the output
     *  without actually compressing it yet, eg:
     *    if (gzip_encode::gzip_accepted()) {
     *       echo "Page will be gziped";
     *    }
     *  note the double colon syntax, I don't know where it is documented but
     *  somehow it got in my brain.
     */
    function gzip_accepted() {
    global $HTTP_ACCEPT_ENCODING;
    if (strpos($HTTP_ACCEPT_ENCODING, 'gzip') === false) return false;
    if (strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') === false) {
        $encoding = 'gzip';
    } else {
        $encoding = 'x-gzip';
    }

    // Test file type. I wish I could get HTTP response headers.
    $magic = substr(ob_get_contents(),0,4);
    if (substr($magic,0,2) === '^_') {
        // gzip data
        $encoding = false;
    } else if (substr($magic,0,3) === 'GIF') {
        // gif images
        $encoding = false;
    } else if (substr($magic,0,2) === "\xFF\xD8") {
        // jpeg images
        $encoding = false;
    } else if (substr($magic,0,4) === "\x89PNG") {
        // png images
        $encoding = false;
    } else if (substr($magic,0,3) === 'FWS') {
        // Don't gzip Shockwave Flash files. Flash on windows incorrectly
        // claims it accepts gzip'd content.
        $encoding = false;
    } else if (substr($magic,0,2) === 'PK') {
        // pk zip file
        $encoding = false;
    }

    return $encoding;
    }

    /*
     * get_complevel() - The level of compression we should use.
     *
     * Returns an int between 0 and 9 inclusive.
     *
     * Tip: $gzleve = gzip_encode::get_complevel(); to get the compression level
     *      that will be used with out actually compressing the output.
     *
     * Help: if you use an OS other then linux please send me code to make
     * this work with your OS - Thanks
     */
    function get_complevel() {
    $uname = posix_uname();
    switch ($uname['sysname']) {
        case 'Linux':
        $cl = (1 - $this->linux_loadavg()) * 10;
        $level = (int)max(min(9, $cl), 0);
        break;
        case 'FreeBSD':
        $cl = (1 - $this->freebsd_loadavg()) * 10;
        $level = (int)max(min(9, $cl), 0);
        break;
        default:
        $level = 3;
        break;
    }
    return $level;
    }

    /*
     * linux_loadavg() - Gets the max() system load average from /proc/loadavg
     *
     * The max() Load Average will be returned
     */
    function linux_loadavg() {
    $buffer = "0 0 0";
    $f = fopen("/proc/loadavg","r");
    if (!feof($f)) {
        $buffer = fgets($f, 1024);
    }
    fclose($f);
    $load = explode(" ",$buffer);
    return max((float)$load[0], (float)$load[1], (float)$load[2]);
    }

    /*
     * freebsd_loadavg() - Gets the max() system load average from uname(1)
     *
     * The max() Load Average will be returned
     *
     * I've been told the code below will work on solaris too, anyone wanna
     * test it?
     */
    function freebsd_loadavg() {
    $buffer= `uptime`;
    ereg("averag(es|e): ([0-9][.][0-9][0-9]), ([0-9][.][0-9][0-9]), ([0-9][.][0-9][0-9]*)", $buffer, $load);

    return max((float)$load[2], (float)$load[3], (float)$load[4]);
    }
}

?>

 

by: cool888Posted on 2003-10-10 at 10:29:54ID: 9529150

thanks for the class file, but how can I check wether that the page is gzipped? Is there a line to show this page is gzipped?


 

by: MarcosBLPosted on 2003-10-10 at 10:39:54ID: 9529204

Just take a look at headers, or simply use this test page:

http://leknor.com/code/gziped.php

If you take a look at the comments in the class file, you will se anexample of use, as well as comments from the autor and this url i have just given you. :)

Sorry if my explain isn't better, is my first response to a question... i don't even know well how all this works : )  If you need something more, just give me a shout.

 

by: jayrodPosted on 2003-10-11 at 09:29:36ID: 9533133

What exactly does this compress marco?  just curious. by the way nice code

 

by: MarcosBLPosted on 2003-10-11 at 11:25:38ID: 9533455

This compresion just compress the code after the page being generated, then sent it to the browser, compressed, and the browser (99,9% of them supports gzip) uncompress the data and show it to you. The benefits ? The transfer of the server reduces drastically, because you only send a medium of 25% of the original data (not counting images, flash, etc, only text, images and so are yet compressed). You pay less for your transfer, and the page reach the visitor about a 25% more quickly, because less data goes trought the line.

And every server usually have the gzip module, the only thing you need to run this class. In my server, for example, i reduced the trasnfer (About 180000 daily hits) from 20 Gb per month to 4,6 Gb per month.

Sorry for my bad english, i am spanish, and self-learned english. If my explanation isn't clear, just say and i'll try to find documentation in english.

 

by: DoppyNLPosted on 2003-10-13 at 03:01:42ID: 9539009

ob_start ("ob_gzhandler");

This should work just fine, no need to use some fancy class.

 

by: DoppyNLPosted on 2003-10-23 at 05:02:48ID: 9605882

Got it working?
Still having some problems?
Did anyone help?

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...