304: Not modified and front end caching

前端 未结 6 660
慢半拍i
慢半拍i 2020-12-13 15:56

I am using a PHP script to serve files. I would like to be able to send back a 304 not modified header in my http response if the file has not changed since th

6条回答
  •  萌比男神i
    2020-12-13 16:26

    There are also some others parameters to check .. in my case I didn't had both of those headers :

    $_SERVER['HTTP_IF_NONE_MATCH'] && $_SERVER['HTTP_IF_MODIFIED_SINCE']
    

    which are required to return a proper 304 header, as my system clock was a little late, It'll interpret those pages as expiring in the future, then not sending those values at all

    Also check that header which is returned by apache, or at least override it to a bigger value

    Cache-Control: max-age=3600
    

    As it won't send previous headers if

    Last-Modified previous sent header < ( NOW - 3600 )
    

    So in my case I've set this pretty handy function function lastModified($file){ $x=filemtime($file); while($x>time())$x-=86000;}#reduce by one day if touched in future date $date=gmdate('D, j M Y H:i:s',$x).' GMT'; header('Cache-Control: max-age=86000',1); if($_SERVER['HTTP_IF_NONE_MATCH'] == $x || $_SERVER['HTTP_IF_MODIFIED_SINCE']==$date){ header('HTTP/1.1 304 Not Modified',1,304);die;} header('Etag: '.$x,1);header('Last-Modified: '.$date,1); }

提交回复
热议问题