HTML - Cache control max age

后端 未结 4 1024
梦毁少年i
梦毁少年i 2021-02-12 14:55

I\'ld like to present always the latest website content to the user but also have it fast loaded. By researching I came across postings people suggesting to use the cache for sp

4条回答
  •  佛祖请我去吃肉
    2021-02-12 15:23

    The Cache-Control header is used in HTTP 1.1 to control the behavior of caches. The max-age directive is used to specify (in seconds) the maximum age of the content before it becomes stale (i.e., the content will not change for some period of time). So if you know that your content will not change for 3 days, you want your server to add the following HTTP header:

    Cache-Control: max-age=259200
    

    (259200 = 60s x 60m x 24h x 3d)

    To do that in PHP, add this line to your output:

    header('Cache-Control: max-age=259200');
    

    Read here for more info on the header function:

    • http://php.net/manual/en/function.header.php

提交回复
热议问题