Delete a header in PHP

2019-06-16 03:32发布

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

4条回答
We Are One
2楼-- · 2019-06-16 03:49

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.

查看更多
Evening l夕情丶
3楼-- · 2019-06-16 03:58

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

查看更多
The star\"
4楼-- · 2019-06-16 03:59

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

查看更多
倾城 Initia
5楼-- · 2019-06-16 04:01

header_remove() since php 5.3

header_register_callback() is also coming soon

查看更多
登录 后发表回答