outputfilter.gzcompress.php
上传用户:xjjlds
上传日期:2015-12-05
资源大小:22823k
文件大小:3k
源码类别:
多媒体编程
开发平台:
Visual C++
- <?php
- /*
- * Smarty plugin
- * -------------------------------------------------------------
- * Type:outputfilter
- * Name:gzip
- * Version:0.1
- * Date:2003-02-13
- * Author:Joscha Feth, joscha@feth.com
- * Purpose:gzip the output, before it is sent to the client
- * ATTENTION: this filter does only work if caching is disabled,
- * because the cached page would be gzipped and it seems as Smarty
- * can not (yet) handle that, even if you send the Content-Encoding yourself.
- * (However, you need not worry about this, if caching is enabled, this filter just returns the source
- * without compression.)
- * Why does it not work with caching (yet)?
- * ----------------------------------------
- * This is because of Smarty adds some information to the beginning of a cached page in ASCII
- * format.
- * Also the file should then be opened in "rb" mode on windows, right now it is (still) opened
- * in "normal" "r" mode.
- * Install: Drop into the plugin directory, call
- * #### $smarty_object->load_filter('output','gzcompress');
- * from application.
- * -------------------------------------------------------------
- */
- function smarty_outputfilter_gzcompress($tpl_source, &$smarty)
- {
- /*~ the compression level to use
- default: 9
- -------------------------------------
- 0->9
- less compressed ->better compressed
- less CPU usage->more CPU usage
- The trade off between CPU usage and compression isn't worth going above level 1 or 2 in most cases.
- 9 will not compress most pages more than a few bytes better than levels 1 & 2, but server load dramatically increases.
- -------------------------------------
- */
- $compression_level = 9;
- /*~ force compression, even if gzip is not sent in HTTP_ACCEPT_ENCODING,
- for example Norton Internet Security filters this, but 95% percent of
- the browsers do support output compression, including Phoenix and Opera.
- default: yes
- */
- $force_compression=false;
- //~ message to append to the template source, if it is compressed
- $append_message = "n<!-- zlib compression level ".$compression_level." -->";
- if(!headers_sent() //~ headers are not yet sent
- && extension_loaded("zlib") //~ zlib is loaded
- && !$smarty->caching //~ caching is disabled
- && (ereg("gzip", $_SERVER["HTTP_ACCEPT_ENCODING"]) || $force_compression))
- {
- //~ correct encoding is sent, or compression is forced
- $tpl_source = gzencode($tpl_source.$append_message, $compression_level);
- header("Content-Encoding: gzip");
- header("Vary: Accept-Encoding");
- header("Content-Length: ".strlen($tpl_source));
- }
- return $tpl_source;
- }
- ?>