用一个内联PHP代码Gzip一个网站 [英] Gzip a website with inline PHP code
问题描述
根据本文
在代码的开头添加它就足够了
it's enough to add this at the beggining of the code
<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>
我尝试过,但是加载时间增加了5秒以上(+ 100%的时间)
I tried, but the loading time has increased more than 5 seconds (+100% time)
这不推荐吗?我可以尝试其他方式吗?
Is this not recomended? shall I try somehow else?
推荐答案
链接文章中给出的代码示例在某种程度上具有误导性(尽管这并没有预先回答您的速度问题).
The code example given in the linked article is somehow misleading (although that's not answering your speed issue upfront).
ob_gzhandler
回调透明地工作,因此仅在浏览器实际支持某种压缩时才有效(不仅像手动检查中那样支持gzip
),因此不需要手动检查.相反,您可以只写:
The ob_gzhandler
callback works transparently, so it's only in effect when the browser actually supports some kind of compression (not only gzip
as in your manual check), so the manual check is not necessary. Instead you can just write:
ob_start("ob_gzhandler");
哪个
ob_start();
如果浏览器不支持压缩.或者,您可以设置相应的ini值.参见 ob_start
文档 和 ob_gzhandler
Docs .
in case the browser does not support compression. Alternatively you can set the corresponding ini value. See ob_start
Docs and ob_gzhandler
Docs.
如果要测量速度差异,请仅将ob_start()
(无压缩)与ob_start("ob_gzhandler")
(压缩)进行比较,而不将缓冲与不进行缓冲进行比较.
If you would like to measure the speed differences, please only compare ob_start()
(no compression) against ob_start("ob_gzhandler")
(compression) and not buffering against not buffering.
然后请记住,gzhandler对块进行操作,因此只有在缓冲了一定数量的数据后,它才能创建输出.可以使用ini设置进行配置,但不能使用回调函数IIRC进行配置.参见 zlib.output_compression
Docs .
Then keep in mind that the gzhandler operates on chunks so it can only create output if there is a certain amount of data buffered. This can be configured with the ini setting, but not the callback function IIRC. See zlib.output_compression
Docs.
这篇关于用一个内联PHP代码Gzip一个网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!