Is my implementation of HTTP Conditional Get answers in PHP is OK?

后端 未结 1 1524
误落风尘
误落风尘 2020-12-09 00:00

After searching a lot, reading every tutorials I\'ve found and asking some questions here, I\'ve finally managed to answer corrctly (at least I think) to if-none-match and i

相关标签:
1条回答
  • 2020-12-09 00:31
    • It's not quite correct. Please take a look at the algorithm: alt text http://img532.imageshack.us/img532/1017/cache.png
    • The solution is proxy-friendly, you may use Cache-control: proxy-revalidate to force caches to obey any freshness information you give them about a resource (only applies to shared|proxy caches)

    Here is the function that might help:

    function isModified($mtime, $etag) {
        return !( (
            isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])
            && 
            strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) >= $mtime
        ) || (
            isset($_SERVER['HTTP_IF_NONE_MATCH'])
            && 
            $_SERVER['HTTP_IF_NONE_MATCH'] == $etag
        ) ) ;
    }
    

    I suggest that you take a look at the following article: http://www.peej.co.uk/articles/http-caching.html

    Update:

    [AlexV] Is is even possible to receive if-none-match AND if-modified-since at the same time?

    You can definitely have both set. However:

    If none of the entity tags match, then the server MAY perform the requested method as if the If-None-Match header field did not exist, but MUST also ignore any If-Modified-Since header field(s) in the request. That is, if no entity tags match, then the server MUST NOT return a 304 (Not Modified) response.

    RFC2616 #14.26

    Example values (W stands for 'weak'; read more in RFC2616 #13.3.3):

    If-None-Match: "xyzzy", "r2d2xxxx", "c3piozzzz"
    If-None-Match: W/"xyzzy", W/"r2d2xxxx", W/"c3piozzzz"
    If-Modified-Since: Sat, 29 Oct 1994 19:43:31 GMT
    If-None-Match: *
    

    As a special case, the value "*" matches any current entity of the resource.

    0 讨论(0)
提交回复
热议问题