Home  >  Article  >  Backend Development  >  PHP sets the browser cache of dynamic web pages

PHP sets the browser cache of dynamic web pages

巴扎黑
巴扎黑Original
2016-11-09 11:21:341282browse

Many people may not know that dynamic web pages can also be cached in browsers. The following uses a PHP script as an example to explain how to set up dynamic web pages to be cached in the browser



//Set the web page expiration time to 1 hour

$duetime = 3600*24*30;


/ /Getting the browser will send the Last-Modified header to the server

$modify_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];


//When the browser accesses the webpage again within the set time, send the HTTP 304 status code ,This saves the amount of data to be transmitted.

if(strtotime($modify_time) + $duetime > time())

{

header('HTTP/1.1 304');

exit(1);

}

header('Connection : keep-alive');

//Set the webpage Last-Modified header

header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');

/ /Set the webpage expiration time

header('Expires: '.gmdate('D, d M Y H:i:s',time()+$duetime).' GMT');

//The execution cache time is long, follow Expires is somewhat similar, allowing us to more comprehensively control the expiration time of web pages, because the browser time may not be coordinated with the server time, and the Cache-Control header can be used to limit it

header('Cache-Control: max-age='.$ duetime);


//Output content

......

?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn