简体   繁体   English

Cake PHP:由于浏览器缓存而引起的问题,站点在我的本地系统中正常运行,但是当移至远程服务器时却无法运行

[英]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. 我正在使用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 我的问题是当调试2站点中的站点正常工作时,如果调试0不能正常运行,我甚至在我的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. 例如:我在登录之前看到一些页面-它显示link1,link2 ...,login(link),登录后进入同一页面时,它应该显示link1,link2 ...,myaccount(link),但仍显示登录。 only after refreshing the page for 2 or 3 time the links or getting changed. 只有在刷新页面2或3次链接或更改后。 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). 第一个告诉浏览器,它可能将此响应保留在其缓存中最多30天(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: 这是请求后30天,你可以从这个标题行看到:

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”和“Expires”标头,以防止浏览器缓存响应:

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 您提供的响应标头包含以下内容,它们会将您的请求结果缓存30天

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. 这两个标头在本质上是等效的,您可以单独使用它们中的任何一个,但是请注意,如果同时使用它们,则max-age具有更高的优先级。

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 在这种情况下,浏览器将始终发送检查ETag值的请求,如果ETag已更改,则服务器将发送新内容,否则将响应状态代码:304 Not Modified

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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