SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard megamanXplosion's Avatar
    Join Date
    Jan 2004
    Location
    Kentucky, USA
    Posts
    1,099
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Use Gzip For Supported Browsers...

    I've been trying to conjur up a method to use GZIP in an effective manner. I want to use GZIP for all browsers which support it. The only browser that actually supports it that I don't want to gzip for is Internet Explorer because it doesn't cache gzip'ed pages. The below function is what I've come up with but it doesn't work and I cannot figure out why. I turned error_reporting to as high as it will go (including notices) but it doesn't show anything wrong. Can someone point out the problem with the function below?

    PHP Code:
    function start_gzip() {
        if(!isset(
    $_SERVER['HTTP_USER_AGENT'])) {
            if(isset(
    $_SERVER['HTTP_ACCEPT_ENCODING'])) {
                if(
    strpos('gzip',$_SERVER['HTTP_ACCEPT_ENCODING'])) {
                    
    ob_start('ob_gzhandler');
                    echo(
    'gzip enabled! ');
                }
            }
        } else {
            if(
    strpos('opera',$_SERVER['HTTP_USER_AGENT'])) {
                
    ob_start('ob_gzhandler');
                echo(
    'gzip enabled!');
            } else if(
    strpos('mozilla',$_SERVER['HTTP_USER_AGENT'])) {
                if(!
    strpos('msie',$_SERVER['HTTP_USER_AGENT'])) {
                    
    ob_start('ob_gzhandler');
                    echo(
    'gzip enabled! ');
                }
            } else if(!
    strpos('msie',$_SERVER['HTTP_USER_AGENT'])) {
                if(isset(
    $_SERVER['HTTP_ACCEPT_ENCODING'])) {
                    if(
    strpos('gzip',$_SERVER['HTTP_ACCEPT_ENCODING'])) {
                        
    ob_start('ob_gzhandler');
                        echo(
    'gzip enabled! ');
                    }
                }
            }
        }

    Hope somebody can help me out here. I had removed GZIP from my website in favor of an enhanced caching system so that Internet Explorer can reek the speed benefits (it sends the 'Last Modified' header to the browser with the date of the latest news article, so it will use it's cache if nothing has been updated but will update it's cache if the page has been). However, I don't want to abandon gzip because the browsers do need to update their caches and I want it to be as fast as possible for when that does happen.

    Anybody care to fix it? I think alot of people will find a working solution to be helpful to them because both caching and gzip is important and it sucks that it's so hard to use the two together...
    Last edited by megamanXplosion; Mar 20, 2004 at 15:12.

  2. #2
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should have used [PHP] for syntax highlighting

    Edit: for strpos to work properly, you need to use check with boolean false
    strpos($findme, $string) !== false
    Speed & scalability in mind...
    If you find my reply helpful, fell free to give me a point

  3. #3
    SitePoint Wizard megamanXplosion's Avatar
    Join Date
    Jan 2004
    Location
    Kentucky, USA
    Posts
    1,099
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, PHP highlighting is now in effect

  4. #4
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this?

    Edit: Silly mistake $findme is the 2nd arg for strpos

    PHP Code:
    start_gzip('GZIP Enabled!');

    function 
    start_gzip($text) {

        if (!isset(
    $_SERVER['HTTP_USER_AGENT']))
        {
            
    $enabled 1;
        }
        else
        {
            
    $http_user_agent strtolower($_SERVER['HTTP_USER_AGENT']);
            
            if (
    strpos($http_user_agent'opera') !== FALSE)
            {
                
    $enabled 1;
            }
            else if (
    strpos($http_user_agent'mozilla') !== FALSE AND strpos($http_user_agent'msie') === FALSE)
            {
                
    $enabled 1;
            }
            else if (
    strpos($http_user_agent'msie') === FALSE)
            {
                
    $enabled 1;
            }
        }
        
        
    ob_start($enabled 'ob_gzhandler' '');
        echo 
    $http_user_agent'<br />', ($enabled $text'GZIP Desabled') ;

    You shouldn't have to check for HTTP_ACCEPT_ENCODING since ob_gzhandler does it already (unless you plan to use you own GZIP compession function)
    Speed & scalability in mind...
    If you find my reply helpful, fell free to give me a point

  5. #5
    SitePoint Wizard megamanXplosion's Avatar
    Join Date
    Jan 2004
    Location
    Kentucky, USA
    Posts
    1,099
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! That seems to work perfectly minus two tiny things which were easily fixed ($enabled was undefined when viewing with IE, and it was set to enable GZIP for Internet Explorer). I've also changed the structure of it a little bit so that it's more readable for me. I'll be adding this to my website on the next minor update, which will hopefully also have SE-friendly URLs and added meta tags. The next minor upgrade should be a good one

    Here's the final code for anyone who wants it (give Daijoubu some reputation points!) for their own site...

    PHP Code:
    <?php
    start_gzip
    ();
    function 
    start_gzip() { 
        
    $enabled 0;
        if (!isset(
    $_SERVER['HTTP_USER_AGENT'])) { 
            
    $enabled 1
        } else { 
            
    $http_user_agent strtolower($_SERVER['HTTP_USER_AGENT']); 
            if (
    strpos($http_user_agent'opera') !== FALSE) { 
                
    $enabled 1
            } else if (
    strpos($http_user_agent'mozilla') !== FALSE AND strpos($http_user_agent'msie') === FALSE) { 
                
    $enabled 1
            } else if (
    strpos($http_user_agent'msie') === FALSE) { 
                
    $enabled 0
            }
        }
        
    ob_start($enabled 'ob_gzhandler' ''); 
    }
    ?>
    Thanks for your help Daijoubu

  6. #6
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    huh, no
    It's === false not !== false

    Google for iehttpheader if you want to check
    Speed & scalability in mind...
    If you find my reply helpful, fell free to give me a point

  7. #7
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Internet explorer will cache gzipped pages - the problem is that it will cache them too often, rather than not often enough. The workaround is not to give IE any cache-control, expires or last-modified headers when sending gzipped content. This would prevent IE from caching them.
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  8. #8
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    huh, IE doesn't cache GZIP'ed page no matter what you do (at least while using ETag)
    Prove me wrong
    Speed & scalability in mind...
    If you find my reply helpful, fell free to give me a point

  9. #9
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    On the contrary, IE always caches GZIPped content no matter what you do.

    The reason for this, as far as I've read, is that IE is unable to unzip the gzipped content in memory due to bad design, so it needs to save it to the temporary internet files and unzip it from there. It also ignores (or rather, 'forgets') Etag, Vary, Cache-Control, Expires, etc for gzipped content, so it can't do conditional gets, and it has no concept of when the cached version expires. The only workaround is not to include any Cache-Control or Expires header, so that IE goes into "always revalidate" mode and the content is never cached.

    In other words, if you're gzipping content, you have no control whatsoever over how long IE caches something and no way of making it do conditional gets. IE becomes 'dumb' with gzipped content.

    http://support.microsoft.com/default...b;en-us;321722

    http://www.sitepoint.com/forums/showthread.php?t=158442
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  10. #10
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even though IE store the file, it won't re-use it (default settings)

    Haven't tested with other settings
    Speed & scalability in mind...
    If you find my reply helpful, fell free to give me a point

  11. #11
    One website at a time mmj's Avatar
    Join Date
    Feb 2001
    Location
    Melbourne Australia
    Posts
    6,282
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Which cache control headers are you using? Do you have a sample URL?
    [mmj] My magic jigsaw
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The Bit Depth Blog Twitter Contact me
    Neon Javascript Framework Jokes Android stuff

  12. #12
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    None, my forum run on a simple GZIP function (gzip header+gzcompress) and none of the page are cached ("re-used")

    Btw, I've replied to your other thread: "IE and its poor HTTP support"
    Speed & scalability in mind...
    If you find my reply helpful, fell free to give me a point


Bookmarks

Posting Permissions

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