?
Solved

How to prevent server from caching included files (PHP Apache)

Posted on 2005-03-22
11
Medium Priority
?
1,828 Views
Last Modified: 2012-06-27
I am developing a web application in php. The site is hosted on an Apache server. I utilize included files quite frequently in my code. I have noticed that even after I upload a new version of an included file, the old version is used for some period of time. This does not seem to happen with the main file itself. Is there a way to prevent this apparent caching of included files on the server? Please note, I am not talking about client (browser) caching.

For example, I have a page called manageUsers.php and near the top of this page I have the following php code:
<?php
require_once('db_incl.php);
?>
db_incl.php contains many database specific functions and code. When I make a change to mangaeUsers.php and upload the new version of the page I view the page in my browser and I usually see the old version, then I it refresh on my browser and I see the new version of the page. When I make a change to db_incl.php and upload the new version, even when I hit refresh on my browser the page behaves as it did before I made the change. When I come back several hours later, the change is reflected on the page.

Obviously this makes development a pain. Is there a way to prevent this apparent caching?

Note that there is nothing in my code to prevent browser caching, and I prefer to stay away from that if possible.

PHP Version: 4.3.9
Apache Version: Apache/1.3.33 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.3.9 FrontPage/5.0.2.2635 mod_ssl/2.8.22 OpenSSL/0.9.7a
Browser Version: IE 6 running on XP Professional (SP2)

0
Comment
Question by:jswright61
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
11 Comments
 
LVL 8

Assisted Solution

by:_Marcel_
_Marcel_ earned 300 total points
ID: 13601943
Two snippets from your request:

> Please note, I am not talking about client (browser) caching.

>Note that there is nothing in my code to prevent browser caching, and I prefer to stay away from that if possible.

Are you really sure that browser caching is not the problem? Unless you are using some sort of optimizer I would say that it is browser caching. The browser will request the URL, which it may have cached. If the server doesn't recognizes that the code has changed (which may be the case if only an include file has been changed), then it will signal the browser to use the cached page, and not download/execute the new page.

For development purposes you may want to set your browser-settings to no cache, but this may give you some other problems, if your site depends on caching. Another way would be to 'touch' the original file, giving it a recent date-time stamp, and so indicating that this is a new file.

It is quite hard to determine what the problem might be, but seems best to firstly rule out browser caching.
0
 
LVL 11

Accepted Solution

by:
matt_mcswain earned 1600 total points
ID: 13601995
>>Browser Version: IE 6 running on XP Professional (SP2)
That's probably the problem.
Try it with Firefox, see if you have the same problem.
Try adding

<?php
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
 
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);

// HTTP/1.0
header("Pragma: no-cache");
?>

add the top of your.
I can see not wanting the prevent caching of large images and the like, but if you have a database driven website, it's not a bad idea to prevent browser caching.
There are other ways to lighten the load on your server, such as server caching of database output.
0
 
LVL 10

Assisted Solution

by:ThaSmartUno
ThaSmartUno earned 100 total points
ID: 13603735
and if you are using IE ... press Ctrl and click refresh ... that adds a nocache to the headers i believe
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jswright61
ID: 13603807
Thanks for the help, I can see the error in my assumption that the server was caching.
I am now strugglig with the header stuff. I have some server side validation on many forms, in order to redirect back to the input form when a validation error occurs I cannot send any headers until after validation is successful. This makes it difficult to place the header cache preventing code (this is why I wanted to stay away from it).
Also I am using sessions and I ahave not modified the session cache limiter (I Checked and it always returns nocache), so do the cache preventing headers do more than the session cache limiter being set to nocache?

Thanks

Scott
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13603975
PHP does not cache stuff like this - I use a PHP accelerator which does caching, but even it does not have this problem - are you sure you're not behind a transparent proxy at your ISP?
0
 
LVL 11

Expert Comment

by:matt_mcswain
ID: 13604448
>>in order to redirect back to the input form when a validation error occurs I cannot send any headers until after validation is successful
Of course you can.
0
 

Author Comment

by:jswright61
ID: 13604761
I Guess that you can
from the php manual:
void header ( string string [, bool replace [, int http_response_code]] )
header() is used to send raw HTTP headers. See the HTTP/1.1 specification for more information on HTTP headers.

reading the above I assumed that header caused headers to be sent, however I tested a page with the cache prevention header code before a header(Location: ) redirect and the redirected page displayed without error (no can't redirect headers sent message). Go Figure!

Thanks Matt - my question is essentially answered and I will be awarding points. I do think that the question about session cache limiter is closely related and I would like clarification on that -
Do the cache prevention header statements do anything more than having a session cache limiter set to nocache?

Thanks

Scott
0
 
LVL 2

Expert Comment

by:TaintedGod
ID: 13605667
The problem is infact that you are using require_once, since the file was already included, refreshing the page will not load of the fresh copy of that file, if you were to switch to require for developing, this would fix your problem. Then switch it back to require_once when the final copy is ready.
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 13605923
Require_once exists to prevent repeated definitions which would otherwise cause a fatal error. It's nothing to do with enhancing performance or caching. Remember that PHP is stateless, so require _once will ALWAYS pick up latest versions.
0
 
LVL 11

Assisted Solution

by:matt_mcswain
matt_mcswain earned 1600 total points
ID: 13606473
>>Do the cache prevention header statements do anything more than having a session cache limiter set to nocache?
session_cache_limiter() returns or sets the session.cache_limiter value in php.ini
The default is 'no cache' and these headers:

Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

This works for most browsers, but some to need be told not to cache pages more than others(IE).
So:

Last-Modified:

appears to be the difference.

In reality, I believe it is a bug in IE and it has problems with the 'Cache-Control' header.
According to M$-->http://support.microsoft.com/kb/q234067/ IE just needs

'Expires -1'




0
 
LVL 11

Expert Comment

by:matt_mcswain
ID: 13606543
>>session_cache_limiter() returns or sets the session.cache_limiter value in php.ini
That's not quite clear; it just changes it during the execution of the script, not the entry in the file itself.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question