tru*_*ktr 1 php compression gzip
我有这个代码:
<?php
// Include this function on your pages
function print_gzipped_page() {
global $HTTP_ACCEPT_ENCODING;
if( headers_sent() ){
$encoding = false;
}elseif( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ){
$encoding = 'x-gzip';
}elseif( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ){
$encoding = 'gzip';
}else{
$encoding = false;
}
if( $encoding ){
$contents = ob_get_contents();
ob_end_clean();
header('Content-Encoding: '.$encoding);
print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
$size = strlen($contents);
$contents = gzcompress($contents, 9);
$contents = substr($contents, 0, $size);
print($contents);
exit();
}else{
ob_end_flush();
exit();
}
}
// At the beginning of each page call these two functions
ob_start();
ob_implicit_flush(0);
// Then do everything you want to do on the page
?>
<html>
<body>
<p>This should be a compressed page.</p>
</html>
<body>
<?
// Call this function to output everything as gzipped content.
print_gzipped_page();
?>
Run Code Online (Sandbox Code Playgroud)
但是当我查看页面源代码时,我没有看到压缩代码.怎么了?
怎么了?
可能没什么.GZIP压缩是服务器和浏览器之间完全透明的过程.服务器将压缩,浏览器将自动解压缩数据.在最终结果(= HTML页面的源代码)中,什么都不会改变.
使用Firebug或Chrome开发人员工具等工具查看响应是否实际压缩.
在Chrome的开发者工具的"网络"标签中,压缩响应将如下所示:
http://fhc.quickmediasolutions.com/image/-1775578843.png