简体   繁体   English

PHP如何不缓存生成的HTML,而是缓存诸如images / js / css之类的静态数据

[英]PHP How to not cache generated HTML but cache static data like images/js/css

Many PHP developers add the no-cache header on top of their PHP pages, so do I, for obvious reasons. 许多PHP开发人员在其PHP页面顶部添加了no-cache标头,出于明显的原因,我也这样做。 Since PHP generated content is usually dynamic, having the browser cache them results in outdated data being presented to the user. 由于PHP生成的内容通常是动态的,因此让浏览器缓存它们会导致向用户呈现过时的数据。 To avoid this caching is usually disabled. 为了避免这种缓存,通常被禁用。

<?php
    //no  cache headers 
    header("Expires: Mon, 26 Jul 1990 05:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
?>

The problem is that due to this header, also my images, javascript files and css files, which are static and thus could (and should) be cached, are also not being cached. 问题是由于这个标头,我的图像,javascript文件和css文件(它们是静态的,因此可以(应该)被缓存)也没有被缓存。 This slows down the site a lot. 这会使网站速度大大降低。

Is there a way to have no cache on the PHP content, but still have cached js/images/css? 有没有办法在PHP内容上没有缓存,但是仍然缓存了js / images / css?

How can this be achieved, assuming I have full access to modify the (linux) server config, HTACCESS and of course, the PHP files themselves? 假设我拥有修改(linux)服务器配置,HTACCESS以及PHP文件本身的完全访问权限,如何实现?

Or is the whole "no-cache thing" unnecessary for dynamic PHP files? 还是动态PHP文件不需要整个“无缓存的东西”? Even when they are url-rewritten to appear extension-less. 即使它们被url重写以显示为无扩展名。

I Might be wrong but I beleive you can bring all your js and CSS files into a single php and then cache that using something like 我可能是错的,但是我相信您可以将所有js和CSS文件都放入一个php中,然后使用类似

ob_start ("ob_gzhandler");
ob_start("compress");

So you would have a .php the defines its header then a few requires on your js and css file's and also the above. 因此,您将拥有一个.php定义其标头,然后在您的js和css文件以及上面的文件中添加一些要求。

You will probobly need a couple of if statements to pickup changes etc. 您可能需要几个if语句来进行更改等。

I will make a more comprehensive answer in a few I am on my phone at the moment. 此刻,我将在电话中提供一些更全面的答案。


UPDATE UPDATE

Found a resource on git that I think will help set you in the right path. 在git上找到了资源,我认为这将帮助您找到正确的道路。

https://github.com/SubZane/CSS-Compressor/blob/master/csscompressor.php https://github.com/SubZane/CSS-Compressor/blob/master/csscompressor.php

you can use a htaccess to define files that you want to be cached all you want is to make a .htaccess file in the main directory of your website and add this code 您可以使用htaccess定义要缓存的文件,只需在网站的主目录中创建一个.htaccess文件,然后添加此代码

example: 例:

# cache images/pdf docs for 1 week
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif)$">
  Header set Cache-Control "max-age=604800, public, must-revalidate"
  Header unset Last-Modified
</FilesMatch>

# cache html/htm/xml/txt diles for 2 days
<FilesMatch "\.(css|js)$">
  Header set Cache-Control "max-age=172800, must-revalidate"
</FilesMatch>

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

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