Can PHP be set to never cache a response, and always send something fresh to the page?
I have a webpage which pulls in an XML news feed and I don't want the page to cache it so that the XML feed displayed is most recent.
I tried this already so the cache is reset every 1800 seconds:
$header[] = "Cache-C开发者_如何学JAVAontrol: max-age=1800";
Didn't seem to work.
So what could I try to make the webpage auto refresh the XML in the PHP?
Try this,
<?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
?>
This will prevent the page from being cached on a user's machine.
Usually in php I set these headers to avoid caching:
header( 'Cache-control: no-cache' );
header( 'Cache-control: no-store' , false );
header( 'Pragma: no-cache' );
header( 'Expires: 0' );
But it is not clear what are you doing. Are you calling a php page that outputs xml?
Client-side, I usally add a parameter (&time=actualtimestamp) to every request so that the browser doesn't cache it.
If you can, do a post request to obtain the XML. Post requests are not cache-able.
If you can not do a post request, add something unique to the query-info part the request URI, like the current time:
old: http://exmaple.com/data.xml
new: http://exmaple.com/data.xml?878621387
^ random number
I first read your question wrong and was concerned about that you want to prevent that a response of yours is being cached. Maybe it's still of use, so I leave it here:
In popular apps there often exists functions that set a bunch of headers to prevent caching. This is an example from the wordpress codebase:
function nocache_headers() {
@ header('Expires: Wed, 11 Jan 1984 05:00:00 GMT');
@ header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
@ header('Cache-Control: no-cache, must-revalidate, max-age=0');
@ header('Pragma: no-cache');
}
It set the Expires
header to a date back in the past. So while being delivered, the document already expired or in caching terms is stale. Those documents should not be delivered by caches.
Then the Last-Modified
header is set to the current time.
Then the Cache-Control
header is set to signal that caches should not cache the response, must revalidate and should keep copies for a maximum age of 0. This sounds a bit schizophrenic maybe (as if not cached, there can't be an age), but that's just to flood proxies and user-agents with data to make them stop.
Finally the Pragma
header is set to no-cache to prevent caching as well for HTTP/1.0 compatible proxies and user-agents (HTTP/1.1 clients normally make use of Cache-Control
).
All these headers are explained in detail in the Hypertext Transfer Protocol -- HTTP/1.1 RFC 2616 Fielding, et al. Section 14 Header Field Definitions if you're eager to checkout the options available.
How are you requesting the XML file? Probably either with cURL or url fopen, right?
Both these methods do not cache the response unless you specifically explicitly ask them to. So you should be fine.
Well probably you have not to set it in php/perl/.. you can also use the htaccess or apache configuration. One example i am using for htaccess: http://snipplr.com/view/4265/cache-control-with-htaccess-expires-by-type/
精彩评论