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

I'm using the following code to get HTTP headers of a website.

<?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. 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).

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/

The relevant headers for caching are Cache-Control and Expires (see sec 14.9.1 of RFC 2616)

In your example above, the server is indicating that it does not wish for the content to be cached ("private, max-age=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. 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. 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.