开发者

PHP remove HTTP header

开发者 https://www.devze.com 2022-12-14 07:29 出处:网络
Something, I think Apache, adds these HTTP headers to all responses generated by PHP scripts: Expires:Thu, 19 Nov 1981 08:52:00 GMT

Something, I think Apache, adds these HTTP headers to all responses generated by PHP scripts:

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

This works ok for actual dynamic pages, but I have some page that, while generated by PHP, are mostly static, and I want the开发者_StackOverflow中文版 browser to cache them.

Is there a way in PHP to remove those headers from the response, and thus activate the browser's default caching rules, or if not, is there any value I can set them to that's equivalent with them being absent?

I would prefer not to set my own values, because I want the browser to use the same caching rules as for static resources that are served by Apache itself (without using mod_cache).


For those particular files you could add header() calls that set those headers differently. ie. header("Expires: " . $currentDatePlus10);

header("Cache-Control: max-age=3600, must-revalidate")


First I'd check if it really isn't one of the php scripts that sets these headers.

register_shutdown_function('foo');
echo "test";

function foo() {
  flush();
  $c = "headers_list: \n  " . join("\n  ", headers_list());

  if ( function_exists('apache_response_headers') ) {
    $c .= "\napache_response_headers:";
    foreach( apache_response_headers() as $k=>$v) {
      $c.= "\n  $k=$v";
    }
  }
  $c .= "\n\n";
  echo '<pre>', $c, '</pre>';
}

Does this print something "usable" on your server?


You can manually provide HTTP headers from PHP via the header() function.

I'd imagine that doing so ought to disable the web server's default header values.


header("Expires: Fri, 1 Jan 2038 05:00:00 GMT");

or some equally absurd time in the distant future. Remember to set your header values before any output has been sent, unless you're doing output buffering for your entire page.

http://php.net/manual/en/function.header.php


These cache headers are sent when you start using sessions and set to "nocache" by default; this makes sure each request gives consistent results.

For instance, if you have a cart system and your browser caches /add?product=xyz, it may not add the product again; this is probably not what you want.

Having said that, the default setting can be changed by either using session_cache_limiter() before session_start() or setting the corresponding session.cache_limiter configuration setting.


There is likely somewhere in your code that has set these variables, as I can't find where they are inserted automatically by PHP anywhere, nor are they in any of my LAMP installations.

The only automatically generated header for my installs is X-Powered-By with the PHP version.

As you've said, from the docs they recommend saying header("Expires:"); to replace the old header, but header("Cache-control:"); just became Cache-Control: max-age=0 in my browser (so this is not what you're trying to do).

I'd recommend checking if these values come from a framework or setting you've changed, but it may be different across different versions of PHP / platforms you'd be running PHP on.

I'd check for ExpiresByType or ExpiresDefault directives in global configs, vhosts, pr .htaccess files or any blocks encapsulated in <IfModule mod_expires> or <IfModule mod_expires.c>

"I want the browser to use the same caching rules as for static resources that are served by Apache itself (without using mod_cache)."

Try looking at a static resource and then matching the rules then. You can calculate the Expires offest with this -> http://www.php.net/manual/en/function.header.php#93377


suppress that of the cache can be made as follows: PHP Code:

header ( "Cache-Control: no-cache, must-revalidate"); / / HTTP/1.1
header ( "Expires: Mon, 1 Jul 1990 05:00:00 GMT"); / / Date in the past

if you want to automatically generate it, then you this here: PHP: session_cache_limiter() - Manual they


If your pages are changing not to often you should consider using Etag headers, like this:

https://gist.github.com/oliworx/4951478

This is useful especially on slow connections (like mobile phones).

Hint: You should always check, what the browser is really loading, with the firefox addon "Live HTTP headers": https://addons.mozilla.org/de/firefox/addon/live-http-headers/


I have not tried this but you could probably save such pages as .html files with your custom headers or lack of their-of and the script could run inside the

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号