Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a header in PHP

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)?

like image 278
Boldewyn Avatar asked Apr 03 '10 17:04

Boldewyn


4 Answers

header_remove() since php 5.3

header_register_callback() is also coming soon

like image 102
goat Avatar answered Oct 09 '22 13:10

goat


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' );.

like image 21
Craig Tullis Avatar answered Oct 09 '22 14:10

Craig Tullis


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()

like image 2
pix0r Avatar answered Oct 09 '22 13:10

pix0r


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.

like image 1
symcbean Avatar answered Oct 09 '22 14:10

symcbean