To allow caching a PHP generated file, I want to make sure, that the 'Pragma: no-cache' header is not set. However, how do I delete a possibly already set header?
That is, it could be possible, that somewhere in the code someone wrote header('Pragma: no-cache');
and now I want to make sure, the header is not sent.
Is it sufficient to do this:
header('Pragma:');
or is there something like delete_header()
(which would, apparently, be undocumented or well-hidden)?
header_remove() since php 5.3
header_register_callback() is also coming soon
I know this question is old and already answered. But some of the answers could leave folks with the wrong impression. Rest assured that if your response headers contain Pragma: no-cache
it absolutely will in fact prevent a web browser from caching a resource regardless of other settings.
So of course if you are using at least PHP 5.3, you can remove the Pragma
header using header_remove( 'Pragma' );
.
You can override a previously set header by passing a second argument to header()
:
void header ( string $string [, bool $replace = true [, int $http_response_code ]] )
Check the manual for header()
The 'pragma' headers behaviours are not defined by the spec - despite the widely held believe that sending a 'Pragma: No-cache' header will have some effect on the browser, in fact it is almost universally ignored (and is never returned by any php installation I've used).
To tell the browser NOT to cache content is done via an expires header with a date in the past, a Cache-Control header with a no-cache value, or (if you want to be sneaky) by a 'Varies: Date' header. In the absence of any of these types of header the client must not cache the page.
So, conversely, if you want a page to be cacheable, set the expires and cache-cntrol headers.
C.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With