简体   繁体   English

如何在没有apache模块的帮助下压缩从多个php源文件生成的html文件?

[英]How to compress a html file with generated from multiple php source file without aid of apache module?

eg. 例如。

file structure 文件结构

+Site
|   +private_files
|   |  header.php
|   |  footer.php
|   |  head.php
|   |  anlytics.php
|   |  blah1.php
|   |  blah2.php
|   |  blah3.php
|   index1.php
|   index2.php
  • every serving file requires header.php , footer.php , head.php , analytics.php . 每个服务文件都需要header.phpfooter.phphead.phpanalytics.php

    besides 除了

  • index1.php requires blah1.php and blah2.php . index1.php需要blah1.phpblah2.php
  • index2.php requires blah2.php , blah3.php . index2.php需要blah2.phpblah3.php

page structure 页面结构

<!DOCTYPE html>
<html>
     <?php include 'private/head.php'?>
<body>
     <?php include 'private/header.php'?>
//rest code
     <?php include 'private/footer.php'?>
</body>
</html>

How compressed HTML files can be served without aid of apache module ? 如何在没有apache模块的帮助下提供压缩的 HTML文件?

I am not very sure about the reason behind "without aid of apache module". 我不太确定“没有apache模块的帮助”背后的原因。 If you mean you do not have the access (or permission) to change apache settings. 如果您的意思是您没有访问权限(或权限)来更改Apache设置。 You can still use gzip from php itself. 你仍然可以使用php本身的gzip。

Just in your header.php (the first included php which could export any content in your script) 就在你的header.php中(第一个包含php,它可以导出脚本中的任何内容)

ini_set('zlib.output_compression_level', 1); //the compress level you want, 1 is lowest
ob_start('ob_gzhandler');

For further reading of ob_gzhandler - http://php.net/ob_gzhandler 进一步阅读ob_gzhandler - http://php.net/ob_gzhandler

If you mean you do not want even use any system command which supported by apache or other system modules. 如果您的意思是您甚至不想使用apache或其他系统模块支持的任何系统命令。 You just look for a solution complete by php itself. 你只需要寻找一个由php本身完成的解决方案。 Yes you can use ob_get_clean() get all the content, then apply some gzip compress to the string, and echo it after. 是的,你可以使用ob_get_clean()获取所有内容,然后对字符串应用一些gzip压缩,然后回显它。 Also, change header to let browser know content is gzip(ed). 另外,更改标题以让浏览器知道内容是gzip(ed)。 However, I do not think you would like the performance. 但是,我不认为你会想要表现。 Besides, you need to lots extra work, feels completely wasted ;-) 此外,你需要额外的工作,感觉完全浪费;-)

For further reading of ob_get_clean - http://php.net/ob_get_clean 进一步阅读ob_get_clean - http://php.net/ob_get_clean

put this before <!DOCTYPE html> <html><?php include 'private/head.php'?> <body> : 把它放在<!DOCTYPE html> <html><?php include 'private/head.php'?> <body>

<?php
   ob_start("ob_gzhandler");
?>

I will break your question into two parts. 我将你的问题分成两部分。 1. Compressing your Css & Js in the site and this can even be done on the fly. 1.压缩站点中的Css&Js,甚至可以即时完成。 Please check out http://manas.tungare.name/software/css-compression-in-php/ 2.Compression of HTML files on the fly. 请查看http://manas.tungare.name/software/css-compression-in-php/ 2.动态压缩HTML文件。 In this case have you tried services like cloudflare. 在这种情况下,您尝试过像cloudflare这样的服务。 They offer compression on the fly. 它们可以即时提供压缩。 A PHP case would involve curl, by creating a new file that first receives the request. PHP案例将涉及curl,通过创建首先接收请求的新文件。 Then the file gets the url content via curl. 然后该文件通过curl获取url内容。 The receive content can then be compressed then echoed. 然后可以压缩接收内容然后回显。 This is pretty crude. 这非常粗糙。

you can use HTML Minify 您可以使用HTML Minify

 $filter = new Zend_Filter_Minify_Html();
 return $filter->filter($view->render($name));

Its not gzip compression , but reduce size of page 它不是gzip压缩,但减少页面大小
Click to Zend Module here 单击此处的Zend模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM