为什么php gzip输出不适合我? [英] Why is php gzip output not working for me?
问题描述
我有此代码:
<?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();
?>
但是当我查看页面源代码时,我看不到压缩代码。
But when i view the page source, I'm not seeing compressed code. WHat's wrong?
推荐答案
错误?
WHat's wrong?
可能没什么。 GZIP压缩是服务器和浏览器之间完全透明的过程。服务器将压缩,浏览器将自动解压缩数据。
probably nothing. GZIP compression is a completely transparent process between the server and the browser. The server will compress, and the browser will automatically uncompress the data. In the end result (= the HTML page's source code), nothing will change.
使用Firebug或Chrome开发者工具等工具来查看响应是否真的被压缩。
Use tools like Firebug or Chrome's developer tools to see whether the response was actually compressed.
在Chrome的开发人员工具的网络标签中,压缩响应将如下所示:
In Chrome's Developer tools' "Network" tab, a compressed response will look something like this:
这篇关于为什么php gzip输出不适合我?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!