dou2347
2011-10-26 01:47
采纳率: 0%
浏览 74
已采纳

使用PHP动态禁用Javascript / CSS的缓存?

I've coded an option called 'devmode' in my web app, which basically means 'no caching'. The app normally outputs an automatically minified (and aggregated) version of the Javascript and CSS, but the devmode option overrides this.

However, we still have the browser cache. So, without further ado, how can I disable caching of ALL components on a page if a certain PHP boolean is true?

Cheers

Edit: might interest you to know that I'm running Apache, and one idea I had was to force .js and .css to be parsed as PHP (which is straightforward), and somehow 'inject' a little piece of PHP code at the start of each.

图片转代码服务由CSDN问答提供 功能建议

我在我的网络应用程序中编写了一个名为“devmode”的选项,这基本上意味着“没有缓存”。 该应用程序通常会输出Javascript和CSS的自动缩小(和聚合)版本,但devmode选项会覆盖此。

但是,我们仍然拥有浏览器缓存。 那么,如果某个PHP布尔值为真,我怎么能禁用页面上所有组件的缓存?

干杯

编辑: 你可能会感兴趣我知道我正在运行Apache,我有一个想法就是强制.js和.css被解析为PHP(这很简单),并且在某种程度上“注入”了一小段PHP代码 每个。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douji6896 2011-10-26 01:55
    已采纳

    A "quick and dirty" approach for debugging/developing, you could call all components in your HTML with a random (or time-based) query-string. For instance:

    <img src="logo.png?uniqecall=20111026035500" />
    

    , which would look like this in your PHP code:

    print '<img src="logo.png?uniqecall=' . date("YmdHis") . '" />';
    

    etc...

    已采纳该答案
    打赏 评论
  • dongsui8162 2011-10-26 02:00

    .htaccess

    RewriteRule ^no-cache/(.*?)$ no-cache.php?file=$1 [QSA,L]
    

    no-cache.php

    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    readfile('static/'.$_GET['file']);
    

    Assuming you won't hack yourself :)

    打赏 评论

相关推荐 更多相似问题