简体   繁体   中英

Cake PHP : Issue due to browser cache, site working properly in my local system, but when moved to remote server its not

I am developing a site using cake php 2.x. My problem is when lode the site in debug 2 site is working properly, if its debug 0 its not.I've even disabled cache in my application in my core.php

 Configure::write('Cache.disable', true);

For example : I am seeing some pages before login - its showing link1, link2 ..., login(link),after login when go the same page it should show link1, link2 ..., myaccount(link) but its still showing login. only after refreshing the page for 2 or 3 time the links or getting changed. But if i go to a page which i've not visited before login it loding properly.

The response header is

array(11) {
  [0]=>
  string(15) "HTTP/1.1 200 OK"
  [1]=>
  string(35) "Date: Tue, 22 Mar 2016 07:30:43 GMT"
  [2]=>
  string(14) "Server: Apache"
  [3]=>
  string(24) "X-Powered-By: PHP/5.5.33"
  [4]=>
  string(124) "Set-Cookie: CAKEPHP=f86bc010f3cc0cafb417ee8651372aa3; expires=Tue, 22-Mar-2016 11:30:43 GMT; Max-Age=14400; path=/; HttpOnly"
  [5]=>
  string(21) "Content-Length: 18892"
  [6]=>
  string(30) "Cache-Control: max-age=2592000"
  [7]=>
  string(38) "Expires: Thu, 21 Apr 2016 07:30:43 GMT"
  [8]=>
  string(32) "Vary: Accept-Encoding,User-Agent"
  [9]=>
  string(17) "Connection: close"
  [10]=>
  string(38) "Content-Type: text/html; charset=UTF-8"
}

Those two header fields control the browser cache:

Cache-Control: max-age=2592000
Expires: Thu, 21 Apr 2016 07:30:43 GMT

The first one tells the browser, it may keep this response in its cache for up to 30 days (30 * 24 * 60 * 60 = 2592000). The second one defines a time, when the cached response should expire. That is 30 days after the request itsself, as you can see from this header line:

Date: Tue, 22 Mar 2016 07:30:43 GMT

You'll have to change the "Cache_Control" and the "Expires" header, to keep the browser from caching the response:

Cache-Control: max-age=0
Expires: Tue, 22 Mar 2016 07:30:43 GMT

The expiration timestamp is the same as the request timestamp now and the browser is told to cache the response for maximum zero seconds. With that information the browser will have to send a new request.

The response headers which you've provided do following stuff, they cache your request result for 30 days

Cache-Control: max-age=2592000
Expires: Thu, 21 Apr 2016 07:30:43 GMT

These two headers are equivalent by it's nature, and you can use any of them independently, but note if you use both, then max-age has higher priority.

If your goal is to achieve caching and proper resource revalidation, then I would suggest you to use following headers:

Cache-Control: max-age=0, must-revalidate
ETag: 'some generated value based on the content' 

In that case browser will always send request to check ETag value, and if ETag has changed then server will sent new content, if not then will respond with Status Code:304 Not Modified

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