+ Reply to Thread
Results 1 to 6 of 6

Thread: How to save 50% - 70% of your bandwidth w/ 1 line of code!

  1. #1
    MyWebs is offline Moderator
    Join Date
    Jun 2008
    Location
    Las Vegas, NV.
    Posts
    57
    WHB Points this Month
    0.00
    WHB Points
    5.00
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up How to save 50% - 70% of your bandwidth w/ 1 line of code!

    While I believe that WHB supports the Apache mod_gzip this is even an easier way to get the same exact bandwidth savings. And it is very portable to other web hosts who don't support this.

    Just add this 1 line of PHP code to all of your .php pages:

    <?php ob_start("ob_gzhandler"); ?>

    on the VERY FIRST LINE of your code. It is very important this is the very first line of code in your pages or it will not work! IF your pages are html you can simply change them from .html to .php and add this line of code to gain the benefits without changing any other code in your pages. Besides saving your hosting account bandwidth it will make downloads of your web pages for dialup users much faster!

    Here are some examples of the difference this 1 line of code made for some of my web pages.

    My Webs home Page
    Original Size: 11 KB
    Gzipped Size: 4 KB
    Data Savings: 63.64% <-- almost 1/3 of original

    MOTAS Game Walkthrough

    Original Size: 40 KB
    Gzipped Size: 12 KB
    Data Savings: 70% <-- A little better than 1/3 of original

    List Of Curse Words this page is #1 in Google and gets Alot of hits so it will help out alot here. Warning don't go to the actuall web page that this link tested if easily offended. It is safe to click this link though to see the test results.

    Original Size: 36 KB
    Gzipped Size: 8 KB
    Data Savings: 77.78% <-- less than 25% of original

    And the best one of all.
    PHP backend for AJAX scripts Warning don't go to the actuall web page that this link tested if easily offended. It is safe to click this link though to see the test results.

    Original Size: 166 KB
    Gzipped Size: 6 KB
    Data Savings: 96.39%

    WOW 3.61% of the original size

    These last 2 pages tested use alot of PHP & MySQL and some javascript / AJAX. So I think if this works on these pages without issues it should work on any page most likely. IF you use a common "Header" file that you include() in all pages like I do on one of my sites then just add the 1 line of code to that "Header" file and instantly your whole web site is GZipped.

    You can read more details from the web page I found this on here:
    GZip Test this page has a tool to check if a web page is using gzip or not. Either way it tells you how much bandwidth gzip is saving or would save if enabled.
    This page explains GZip in more detail and lists the 1 line of code I mention. PHP GZip

    Hopefully this will open other users eyes to how much of a difference gzip can make. It would seem on average you can expect a savings in the range of 50% - 70% but even much better is possible as I have shown. Both Google and Yahoo use this and since 99% of browsers support this there is no reason you shouldn't either. And if a browser doesn't support GZip then they get uncompressed data instead. WIN - WIN for both you and your customers!
    Thank you,
    MyWebs
    Last edited by MyWebs; 06-16-2008 at 06:47 AM. Reason: Typo
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Get published on a top 100 web site where they share revenue.
    Take The Tour to learn more. (I am compensated for referral)
    Then sign up for free to Make Money Writing. (Always Free)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  2. #2
    Matt R.'s Avatar
    Matt R. is offline WeeHBie
    Join Date
    Jul 2006
    Posts
    1,394
    WHB Points this Month
    0.00
    WHB Points
    60.00
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    Good article. We'll look to include this in our Wiki.
    Matt Russell
    WebHostingBuzz CEO

    Follow me on Twitter: http://www.twitter.com/mattdrussell

  3. #3
    MyWebs is offline Moderator
    Join Date
    Jun 2008
    Location
    Las Vegas, NV.
    Posts
    57
    WHB Points this Month
    0.00
    WHB Points
    5.00
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks Matt

    I should add this php code only GZips up your text output. (X)HTML, XML or whatever. It isn't going to do anything for your images or external CSS - javascripts. For that you would need to enable GZip for all of your content via "Optimize Website" from within cPanel.

    For what its worth GZip'ing your images isn't going to make much of a difference as .jpg, .gif and .png are already heavily compressed. It would if you was using .bmp but hopefully no one here is using them. Text compresses the best, especially pages with alot of HTML or XML code like my one example that compressed down to 3.61%.

    Thanks,
    Tony

  4. #4
    Alan B's Avatar
    Alan B is offline Super Moderator
    Join Date
    Jul 2007
    Location
    Toronto, Canada
    Posts
    1,386
    WHB Points this Month
    0.00
    WHB Points
    290.00
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default

    I would advise anyone wanting to use this to test it well. I've read reports of IE6 displaying blank pages rather than the indended content.

    I'm not criticizing this technique, just warning that it should be tested with a variety of client platforms and browsers. I wouldn't want to see a bunch of posts asking why websites aren't displaying, because the client forgot that he implemented this technique.

  5. #5
    omniuni's Avatar
    omniuni is offline Jewbergeek!
    Join Date
    Jul 2007
    Posts
    181
    WHB Points this Month
    0.00
    WHB Points
    0.00
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    If you want, you can simply use PHP to detect IE6, first. I havent tested this completely, but I believe it should work:

    <?php
    //Set isIE6 variable and set to false.
    $isIE6 = false;
    if (preg_match('|MSIE ([0-6].[0-9]{1,2})|',$useragent)) {
    //If browser ID contains MSIE(0-6) set $isIE6 to true.
    $isIE6 = true;
    }
    if(!$isIE6){
    //If $isIE6 is false, it is safe to start gzhandler. Do so:
    ob_start("ob_gzhandler");
    }else{
    //Put a comment in the source noting that the website is uncompressed.
    echo "<!-- IE6 has trouble with compressed websites. Sending as uncompressed. \n
    To increase performance, use a non-Microsoft browser. -->";
    }
    ?>

    -Omni
    Last edited by omniuni; 06-16-2008 at 07:17 PM.

  6. #6
    trulymoney_com is offline New Bee
    Join Date
    Sep 2006
    Location
    TrulyMoney.com
    Posts
    4
    WHB Points this Month
    0.00
    WHB Points
    0.00
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb

    Quote Originally Posted by Alan B View Post
    I would advise anyone wanting to use this to test it well. I've read reports of IE6 displaying blank pages rather than the indended content.

    I'm not criticizing this technique, just warning that it should be tested with a variety of client platforms and browsers. I wouldn't want to see a bunch of posts asking why websites aren't displaying, because the client forgot that he implemented this technique.
    Firstly, you should not use the gzip compression for your front page.

    Secondly, on your front page you give the visitors an option to use such gzip compression including notice to visitors that if there is a blank page appear after selecting the 'compressed version' then it most likely the browser can not handle such compression so they can back to the front page and choose 'non-compressed version'. Save the setting in session, say as 'gzip', if the visitors choose 'compressed version' otherwise unset the present set session of gzip. Well, this is just an idea.

    <?php
    session_start();

    if (isset($_SESSION['gzip'])) ob_start("ob_gzhandler");
    ?>

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts