Home > php教程 > php手册 > body text

php header缓存的示例

WBOY
Release: 2016-06-06 19:38:24
Original
1251 people have browsed it

每5秒更新.. Adminers ?php$interval=5;session_cache_limiter('private_no_expire'); //在session之后处理缓存, 需要加上这个.session_start();if ($_SERVER['HTTP_IF_MODIFIED_SINCE']){ // HTTP_IF_MODIFIED_SINCE即下面的: Last-Modified,文档缓存时间. /

每5秒更新.. Adminers
<?php
$interval=5;
session_cache_limiter('private_no_expire'); //在session之后处理缓存, 需要加上这个.
session_start();

if ($_SERVER['HTTP_IF_MODIFIED_SINCE']){
    // HTTP_IF_MODIFIED_SINCE即下面的: Last-Modified,文档缓存时间.
    // 缓存时间+时长. 
    $ctime = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])+$interval;
         
    // 当大于当前时间时, 表示还在缓存中... 释放304
    if($ctime > time()){
       header('HTTP/1.1 304 Not Modified');
       exit();
    }
}

// 上面这段代码未使用任何其它库, 所以可以放在首行.
 
header("Pragma: private");
header("Cache-Control:max-age=$interval, pre-check=$interval"); 
header("Expires: " . gmdate("D, d M Y H:i:s",time()+$interval)." GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
 
 
echo date('Y-m-d H:i:s'); // 注意看是不是差5秒刷新一次.
echo ' - <a href="?a=">Link</a><br />';
var_dump($_SERVER['HTTP_IF_MODIFIED_SINCE']);

// IE11 ff38 chrome测试通过.
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!