gpt4 book ai didi

php - 缓存php解析的css、js、txt文件

转载 作者:搜寻专家 更新时间:2023-10-31 21:18:00 26 4
gpt4 key购买 nike

问题

我正在进行一些实验,需要一些帮助。
我创建了 2 个文件。 ma​​in-real.css 是标准的普通 ol' css 文件,ma​​in.css 由 PHP 解析并具有 include() 它抓取了以前的真实 css 文件。
以下是 ma​​in.css 的代码:

<?php 
include("main-real.css");
?>

然后我在我的 .htaccess 文件中添加一条指令,以使用 PHP 解析此 css 文件:

<FilesMatch "main.css">
AddHandler application/x-httpd-php5 .css
Header Set Content-Type "text/css"
</FilesMatch>

这在我运行 Apache 的 PHP 5.2 服务器上完美运行。
问题是这个文件似乎没有被浏览器缓存,或者至少没有返回一个
304 Not Modified 状态代码,类似于常规的未经 PHP 解析的 CSS 文件。

如果直接访问,这里是 ma​​in-real.css 的 header :

RESPONSE HEADERS    Date..............Thu, 18 Nov 2010 22:10:57 GMT    Server............Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635    Last-Modified.....Thu, 18 Nov 2010 22:10:23 GMT    Etag.............."11b010a-26-4955b0e6671c0"    Accept-Ranges.....bytes    Content-Length....38    Content-Type......text/cssREQUEST HEADERS    Accept.............text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8    Accept-Language....en-us,en;q=0.5    Accept-Encoding....gzip,deflate    Accept-Charset.....ISO-8859-1,utf-8;q=0.7,*;q=0.7    Keep-Alive.........115    Connection.........keep-alive    Cookie.............fc=fcVal=7625790752294348480    If-Modified-Since..Thu, 18 Nov 2010 22:10:23 GMT    If-None-Match......"11b010a-26-4955b0e6671c0"    Cache-Control......max-age=0

Here are the headers for the PHP parsed main.css:

RESPONSE HEADERS    Date...............Thu, 18 Nov 2010 22:11:11 GMT    Server.............Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635    X-Powered-By.......PHP/5.2.11    Content-Type.......text/css    Keep-Alive.........timeout=5, max=97    Connection.........Keep-Alive    Transfer-Encoding..chunkedREQUEST HEADERS    Accept.............text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8    Accept-Language....en-us,en;q=0.5    Accept-Encoding....gzip,deflate    Accept-Charset.....ISO-8859-1,utf-8;q=0.7,*;q=0.7    Keep-Alive.........115    Connection.........keep-alive    Cookie.............fc=fcVal=7625790752294348480    Cache-Control......max-age=0

I have tried modifying the http-headers in all sorts of ways, adding max-age, last-modified and others with no success. Is there something I am missing or misunderstanding?


Solution & Final Code

The main missing piece of code was that I needed to send the Last-Modified header prior to the include(). This needs to be done within the PHP file itself! I previously tried adding Last-Modified using an .htaccess Header set instruction, and although that does add the appropriate header, it did not trigger caching.
Here is my final code for main.css with far-future Expires headers and Cache-Control for good measure.

<?php
$last_modified = date("D, d M Y H:i:s \G\M\T", filemtime("main-shared.css"));
$expiration = date("D, d M Y H:i:s \G\M\T", strtotime('+1 year'));

header("Cache-Control: public, no-transform");
header("Expires: $expiration");
header("Last-Modified: $last_modified");

include("main-shared.css");
?>

最佳答案

Apache 将为 main-real.css 发送什么 header 是无关紧要的,因为您正在通过文件系统include() ing 该文件。

在包含其他文件之前,您需要通过 PHP 脚本发送相同的 header 。

header("Cache-Control: ........ ");
header("Expires: ....... ");
....
include("main-real.css");

关于php - 缓存php解析的css、js、txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4220318/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com