简体   繁体   中英

Local LAMP stack, prevent PHP from caching dynamically loaded pages

I have a local LAMP stack running on Linux. I have the usual Apache/PHP/mySQL stuff, and everything works fine from localhost.

I have a small app on the server that displays a tree view. This lists a series of documents that are found locally (on the server). These files can be viewed by clicking them in the tree, at which point they are loaded into a DIV on the page. These files may be edited using a simple editor that I have written within the application. This works fine so far.

The problem is that PHP (I assume) is caching these documents as they are loaded, which means that when the user edits them, although they are saved, when they are reselected in the tree, the cached version is shown, and not the newly edited version.

The initial page of the app was a standard HTML page, but I have renamed it to PHP and added the following code to the top of this page:

<?php
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");
?>

Unfortunately, while this seems to prevent the main page from being cached, it does not prevent those files that are loaded dynamical during the general use of the application.

Is there any way that I can prevent PHP (Still assuming that it is PHP) from caching these dynamically loaded, local files?

You're mixing up opcode cache and browser cache. The headers you are setting have nothing to do with PHP, they are http headers which influence the browser caching. PHP will (if it's setup properly with APC or similar) cache the opcode, which has nothing to do with your problem.

You have to set the caching headers on the file when you download them to the client for editing in order to prevent the client (browser) to cache them.

Every download (css file, js file, whatever file) is a separate http request and therefore has its own headers set. So if you set custom per-request headers you'll have to set them for each such request. In your case an easier solution would be to add those headers on the level of the Apache configuration.

The Apache Header directive is easy to use and you can use it almost anywhere in the Apache configuration chain. That means .htacess, vhost config or general apache config. It's up to you and your server environment, how you solve this.

An example of how you could add a header to your .htaccess would be:

Header set Cache-control "max-age=0, no-store"

Or if you wish to set certain headers only for certain file types:

<FilesMatch "\.(gif)$" >
    Header set Cache-control "max-age=0, no-store"
</FilesMatch>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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