PHP如何不缓存生成的HTML,但缓存静态数据,如images / js / css

时间:2021-06-11 04:52:19

Many PHP developers add the no-cache header on top of their PHP pages, so do I, for obvious reasons. Since PHP generated content is usually dynamic, having the browser cache them results in outdated data being presented to the user. To avoid this caching is usually disabled.

许多PHP开发人员在他们的PHP页面之上添加了no-cache标头,所以我也是如此,原因很明显。由于PHP生成的内容通常是动态的,因此让浏览器缓存它们会导致向用户呈现过时的数据。为避免这种情况,通常会禁用缓存。

<?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. This slows down the site a lot.

问题是由于这个标题,我的图像,javascript文件和css文件,也是静态的,因此可以(并且应该)被缓存,也没有被缓存。这会大大减慢网站的速度。

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? Even when they are url-rewritten to appear extension-less.

或者动态PHP文件不需要整个“无缓存的东西”?即使它们被重写为url,也显示无扩展。

2 个解决方案

#1


0  

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>

#2


2  

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

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

#1


0  

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>

#2


2  

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

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