简体   繁体   中英

header(“Cache-Control:max-age = 36000”) Is not working in PHP

I want to cache html page in my browser , and i am tying it on localhost , And I am sending the correct header( using the PHP) in response header but still browser is not caching the response, and every time i request same resource, It connect to server and get response from there

At top of my html page I am using

 <?php 
header("Cache-Control:max-age=36000");
  ?>

And the Response headers are

HTTP/1.1 200 OK

Date: Tue, 15 Nov 2016 14:45:37 GMT

Server: Apache/2.4.16 (Win32) OpenSSL/1.0.1p PHP/5.6.12

X-Powered-By: PHP/5.6.12

Cache-Control: max-age=36000

Accept-Ranges: none

Vary: Accept-Encoding

Content-Encoding: gzip

Content-Length: 154

Keep-Alive: timeout=3, max=100

Connection: Keep-Alive

Content-Type: text/html; charset=UTF-8

When i saw Cache-Control:max-age=36000 in headers , I was expecting browser will cache this response for 36000 seconds and if i reload page ,I will get the cached response (and different response header) , but i am getting same header after reload ,and getting response straight from server again ,,

after reload request headers are

GET /check.php HTTP/1.1
Host: localhost
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML,    like Gecko) Chrome/54.0.2840.71 Safari/537.36
Accept:     text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8

Should i send any other response header for tell browser to cache the response ?

PHP (of course) adds some magical cache-control headers by itself. It is not possible to simply overwrite those with header(), and you have to use session_cache_limiter() to set different cache control headers, or session_cache_limiter('') to disable those magical headers all together..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM