您可以做的最好的事情之一是删除任何不需要内联的内联脚本。
我在我的 PHP 文件的顶部有这个:
ob_start();
ob_implicit_flush(0);
然后我包括这个功能:(我的笔记没有说我从哪里偷来的)
function print_gzipped_page() {
$HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"];
if( headers_sent() )
$encoding = false;
else if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false )
$encoding = 'x-gzip';
// *** I can't recall why I disabled this one ***
// I had some device that it didn't work with.
//else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false )
// $encoding = 'gzip';
else {
$encoding = false;
}
$contents = ob_get_clean();
if ($encoding)
{
$_temp1 = strlen($contents);
if ($_temp1 < 2048) { // no need to waste resources in compressing very little data
print($contents);
} else {
header('Content-Encoding: '.$encoding);
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$contents = gzcompress($contents, 9);
$contents = substr($contents, 0, $_temp1);
print($contents);
}
}
else {
print($contents);
}
}
在页面的最底部,我只是调用:
print_gzipped_page();
正如代码所说,如果您已经发送了标头 - 如果您已经发送了任何输出,基本上,那么此代码不会为您压缩任何内容。