压缩和缩小PHP缓存页面

时间:2019-01-11 11:02:02

标签: php caching compression gzip minify

我正在使用以下here中所述并显示如下的页面缓存技术:

<?php
$cachefile = 'cached-files/'.date('M-d-Y').'.php';
$cachetime = 18000;

// Check if the cached file is still fresh. If it is, serve it up and exit.
if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) 
{
    include($cachefile);
    exit;
}

// no file OR the file to too old, render the page and capture the HTML.
ob_start( 'ob_gzhandler' );
?>

<html>
    <!-- CONTENT GOES HERE -->
</html>

<?php
// Save the cached content to a file
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);

// finally send browser output
ob_end_flush();
?>

一切正常,但是我想压缩和缩小缓存的文件。

我已经将ob_gzhandler添加到了

ob_start( 'ob_gzhandler' );

并拥有一个包含以下内容的htaccess文件:

<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

<IfModule mod_expires.c>
  ExpiresActive on

# Your document html
  ExpiresByType text/html "access plus 0 seconds"

# Media: images, video, audio
  ExpiresByType audio/ogg "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/ogg "access plus 1 month"
  ExpiresByType video/webm "access plus 1 month"

# CSS and JavaScript
  ExpiresByType application/javascript "access plus 1 week"
  ExpiresByType text/css "access plus 1 week”

# Fonts
  AddType application/vnd.ms-fontobject .eot
  AddType application/x-font-ttf .ttf
  AddType application/x-font-opentype .otf
  AddType application/x-font-woff .woff
  AddType image/svg+xml .svg

  AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-opentype image/svg+xml

  ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
  ExpiresByType application/x-font-ttf "access plus 1 year"
  ExpiresByType application/x-font-opentype "access plus 1 year"
  ExpiresByType application/x-font-woff "access plus 1 year"
  ExpiresByType image/svg+xml "access plus 1 year"
</IfModule>

但是,当我使用任何在线工具检查gzip压缩时,它们都回来了。 我想念什么吗?

还有没有办法缩小ob_get_contents中包含的缓存HTML?

2 个答案:

答案 0 :(得分:0)

您在这里所做的事情有很多问题。

通常,您在此处向我们展示的Apache配置应该处理压缩-但这取决于处理程序的堆叠顺序。鉴于必须处理为当前请求生成内容,输出缓冲区和重放存储的文件的复杂性,如果您的PHP代码只是忽略压缩并且由Web服务器处理,则您的生活将变得简单得多。因此,开始尝试一次分析一个问题:查看当您请求简单的PHP生成的HTML时Web服务器的响应。如果未压缩,请返回并查看您的Web服务器(是否检查过mod_delfate是否实际加载成功?)。

  

压缩并缩小PHP缓存页面

压缩通过减少冗余来工作。缩小通过减少冗余来工作。压缩将使大小减少约80%。除非您的HTML包含很多冗余标记(即“ <tag></tag>”),并且您的缩小器足够聪明地识别并删除这些标记(我所见不到的)或大量的空格,否则缩小只会减少大小最多增加5%。这些效果不是累加的。

同时使用这两种方法会给代码增加很多成本(处理,编程)和复杂性,因此不会带来任何好处。

  

还有一种方法可以减少缓存的HTML

那太傻了。

您不能假定客户端将接受压缩的内容,也不能假设它们将支持哪种压缩方法。您可能知道所有流量都流向支持gzip的Web浏览器-但您不知道有多少浏览器使用的是大脑损坏的AV软件或其他代理来中介连接。因此,如果您将缓存的表示形式保持压缩状态,则需要重新实现围绕检测客户端功能的逻辑,并允许对代码进行解压缩。

命名模式仅容纳单个页面/缓存文件,而是表明您没有大量数据要存储在服务器端缓存中,并且磁盘空间很便宜。存储未压缩的缓存数据。

  

ExpiresByType文本/ html“访问加上0秒”

这充其量是多余的。

  

$ cachefile ='cached-files /'。date('M-d-Y')。'。php';

如果您不是很谨慎地防止XSS,那么您可能会遇到一个巨大的安全问题-XSS漏洞可以用作服务器上的远程代码注入漏洞。

  

include($ cachefile);

如果您知道文件应该仅包含HTML,那么为什么要让PHP解析/编译/执行它呢?除了安全性问题之外,它还会增加处理开销,并会导致您的操作码缓存不断填充和重置。

还有可能有人在写入缓存文件之前拔掉请求上的插头来破坏您的缓存。

考虑:

 <?php
 ignore_user_abort(1);
 ...
 if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) 
 {
     while (ob_get_level()) ob_end_flsuh();
     readfile($cachefile);
     exit;
 }
 ...

答案 1 :(得分:0)

很容易解决您的问题。我也遇到了同样的问题,为此,您必须预料到cachefil函数的压缩方法,修复后的代码将如下所示:

<?php

    /* importat */
 ob_start ('ob_gzhandler');

 /* importat */ 

    $ cachefile = 'cached-files /'. date ('M-d-Y'). '. php';
    $ cachetime = 18000;

    // Check if the cached file is still fresh. If it is, serve it up and exit`enter code here`.
    if (file_exists ($ cachefile) && time () - $ cachetime <filemtime ($ cachefile))
    {
         include ($ cachefile);
         exit;
    }

    // no file OR the file to too old, render the page and capture the HTML.

    ?>

    <html>
         <! - CONTENT GOES HERE ->
    </html>

    <? php
    // Save the cached content to a file
    $ fp = fopen ($ cachefile, 'w');
    fwrite ($ fp, ob_get_contents ());
    fclose ($ fp);

    // finally send browser output

?>

不客气