简体   繁体   English

从HTTP标头检查网站是否已启用缓存

[英]Check if website has cache enabled or not from HTTP Headers

I'm using the following code to get HTTP headers of a website. 我正在使用以下代码来获取网站的HTTP标头。

<?php
$url = "http://www.google.com/";
$headers = get_headers($url);
$code = $headers[0];
print_r($headers);    
?>

The above code displays the output: 上面的代码显示输出:

Array ( 
    [0] => HTTP/1.0 302 Found 
    [1] => Cache-Control: private 
    [2] => Content-Type: text/html; charset=UTF-8 
    [3] => Location: http://www.google.co.in/?gfe_rd=cr&ei=6Ge_VvG0JKTv8wekkIegCA 
    [4] => Content-Length: 261 
    [5] => Date: Sat, 13 Feb 2016 17:29:12 GMT 
    [6] => HTTP/1.0 200 OK 
    [7] => Date: Sat, 13 Feb 2016 17:29:12 GMT 
    [8] => Expires: -1 
    [9] => Cache-Control: private, max-age=0 
    [10] => Content-Type: text/html; charset=ISO-8859-1 
    [11] => P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info." 
    [12] => Server: gws 
    [13] => X-XSS-Protection: 1; mode=block 
    [14] => X-Frame-Options: SAMEORIGIN 
    [15] => Set-Cookie: NID=76=ap8f4I3nvVUaV7rYQYL88Un1P5ctbb-SPDcn7Zq1UYXkqb-mcQUD9gtrJsn2v67hUiTVT62xDebimSvxL__EzsQrf9Er_cUP9wnv7NVJcS0FgOEj0enKgzu0o6zKOyBF; expires=Sun, 14-Aug-2016 17:29:12 GMT; path=/; domain=.google.co.in; HttpOnly 
    [16] => Accept-Ranges: none 
    [17] => Vary: Accept-Encoding 
)

How can I know whether this website has browser caching enabled or not from this headers? 如何通过此标题知道该网站是否启用了浏览器缓存?

The particular header you've posted is a 302 redirection to http://www.google.co.in/?gfe_rd=cr&ei=6Ge_VvG0JKTv8wekkIegCA but it is also not being cached (for good reason in this case) as seen by the Expires and Cache-Control headers set to -1 and max-age=0 . 您发布的特定标头是302重定向到http://www.google.co.in/?gfe_rd=cr&ei=6Ge_VvG0JKTv8wekkIegCAExpires也未将其缓存(在这种情况下,有充分的理由)和Cache-Control标头设置为-1max-age=0 So if you are trying to detect if a site is sending these caching headers then you can check for those but not all sites use the same technique and some use incorrect headers so you might have to check for a number of them. 因此,如果您尝试检测某个站点是否正在发送这些缓存头,则可以检查那些站点,但并非所有站点都使用相同的技术,并且某些站点使用不正确的头,因此您可能必须检查多个头。

Here is an example of a 200 response that is sending the right headers related to caching ( Cache-Control , Etag , Expires to name a few). 这是一个200响应的示例,该响应正在发送与缓存有关的正确标头( Cache-ControlEtagExpires )。

HTTP/1.1 200 OK
Content-Encoding: gzip
Accept-Ranges: bytes
Cache-Control: max-age=604800
Content-Type: text/html
Date: Sat, 13 Feb 2016 18:15:19 GMT
Etag: "359670651+gzip"
Expires: Sat, 20 Feb 2016 18:15:19 GMT
Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
Server: ECS (cpm/F9D5)
Vary: Accept-Encoding
X-Cache: HIT
x-ec-custom-error: 1
Content-Length: 606

May be useful also: https://www.mnot.net/cache_docs/ 可能也有用: https : //www.mnot.net/cache_docs/

The relevant headers for caching are Cache-Control and Expires ( see sec 14.9.1 of RFC 2616 ) 缓存的相关标头是Cache-Control和Expires( 请参阅RFC 2616的14.9.1节

In your example above, the server is indicating that it does not wish for the content to be cached ("private, max-age=0"). 在上面的示例中,服务器指示不希望缓存内容(“私有,最大年龄= 0”)。 Additionally, a "-1" Expires (which is a bit non-standard, as the "Expires" header is meant to contain an actual date) seems to indicate that the content has already expired. 此外,“-1”到期(似乎有点不标准,因为“到期”标头旨在包含实际日期)似乎表明内容已经到期。 The "private" by itself would actually indicate that the browser could cache the content, but no intermediate proxy servers. “私有”本身实际上将指示浏览器可以缓存内容,但没有中间代理服务器。

private Indicates that all or part of the response message is intended for a single user and MUST NOT be cached by a shared cache. private表示全部或部分响应消息是针对单个用户的,并且不得由共享缓存缓存。 This allows an origin server to state that the specified parts of the response are intended for only one user and are not a valid response for requests by other users. 这允许源服务器声明响应的指定部分仅面向一个用户,而不是针对其他用户的请求的有效响应。 A private (non-shared) cache MAY cache the response. 私有(非共享)缓存可以缓存响应。

It's not always straightforward as there are multiple options that may be contained in the Cache-Control headers to indicate which intermediate servers might cache. 这并不总是那么简单,因为Cache-Control标头中可能包含多个选项以指示哪些中间服务器可以缓存。

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

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