views:

62

answers:

1

So, I'm reading through the YUI best practices for speeding up your web page, and I'm using PHP to try and implement some of those suggestions. Here is what I have so far:

<?php 
// Expires one year from now
$expires = mktime(0, 0, 0, date("m"),   date("d"),   date("Y")+1);
// Format date
$date =  date('D, d M Y H:i:s', $expires);
// Send HTTP header
header("Expires: $date GMT");
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) 
    ob_start("ob_gzhandler"); 
else 
    ob_start();
?>
<!DOCTYPE ....
...
</head>
<?php flush(); ?>
<body>
...

So, does the above look good?

I have two specific questions.

  1. Is the ob_start(); after the else necessary.
  2. If the flush() doing any good at all (or possibly harm?)

    One of the suggestions is that you flush() your page, yet another suggestion is that you GZIP your page. It makes sense that you can't flush a GZIPPED page, since the whole page is one big bundle right?

    What happens if you use flush() on a page that is GZIPPED? Can anything "bad" happen? Should you keep flush() in your page for the benefit of browsers that don't accept GZIPPED content?

Thanks.

+2  A: 
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) 
    ob_start("ob_gzhandler"); 
else 
    ob_start();

You can simply write:

ob_start("ob_gzhandler");

It will automatically determine whether the browser supports gzip/deflate ;)

And as you enabled Output Buffering your page will be flushed as a whole chunk, therefore flush will not have an effect. But it won't harm either. It simply will flush nothing ;)

nikic
@nikic - Thanks - re:Expires I create `$expires`, then I modify the format with `date()` and store it in `$date`, and I finally do send it with, `header("Expires: $date GMT")`
Peter Ajtai
Oh, sorry, didn't see that ;)
nikic