首頁 >php教程 >php手册 >文件缓存,php文件缓存

文件缓存,php文件缓存

WBOY
WBOY原創
2016-06-13 08:44:331592瀏覽

文件缓存,php文件缓存

不多说,看代码

Head.php

<?<span>php
define(</span><span>'</span><span>CACHE_EXPIRE</span><span>'</span>,<span>1800</span><span>);  //设置过期时间
define(</span><span>'</span><span>CACHE_PATH</span><span>'</span>,$_SERVER[<span>'</span><span>DOCUMENT_ROOT</span><span>'</span>].<span>'</span><span>/cache/</span><span>'</span><span>); //设置缓存目录
define(</span><span>'</span><span>REFRESH_EXPIRE</span><span>'</span>,<span>10</span><span>);  
is_dir(CACHE_PATH) or mkdir(CACHE_PATH,</span><span>0777</span><span>);
<br />//该方法是获得用户的刷新次数,如果用户连续两次刷新,那么就显示最新内容
function refreshTimes(){
    $currentUrl</span>=<span>'</span><span>http://</span><span>'</span>.$_SERVER[<span>"</span><span>HTTP_HOST</span><span>"</span>].$_SERVER[<span>'</span><span>REQUEST_URI</span><span>'</span><span>];
    </span><span>if</span>(isset($_COOKIE[<span>'</span><span>refreshTimes</span><span>'</span><span>])){
        $cookArray</span>=explode(<span>"</span><span>\t</span><span>"</span>,$_COOKIE[<span>'</span><span>refreshTimes</span><span>'</span><span>]);
        </span><span>if</span>(isset($cookArray[<span>1</span>]) and $_SERVER[<span>'</span><span>REQUEST_TIME</span><span>'</span>] - (<span>int</span>)$cookArray[<span>1</span>] < REFRESH_EXPIRE and isset($cookArray[<span>0</span>]) and $cookArray[<span>0</span>] ==<span> $currentUrl){
            setcookie(</span><span>'</span><span>refreshTimes</span><span>'</span>,$currentUrl.<span>"</span><span>\t</span><span>"</span>.$_SERVER[<span>'</span><span>REQUEST_TIME</span><span>'</span>].<span>"</span><span>\t</span><span>"</span>.(<span>string</span>)((<span>int</span>)$cookArray[<span>2</span>]+<span>1</span>),<span>0</span>,<span>'</span><span>/</span><span>'</span><span>);
            </span><span>return</span> (<span>int</span>)$cookArray[<span>2</span><span>];
        }</span><span>else</span><span>{
            setcookie(</span><span>'</span><span>refreshTimes</span><span>'</span>,$currentUrl.<span>"</span><span>\t</span><span>"</span>.$_SERVER[<span>'</span><span>REQUEST_TIME</span><span>'</span>].<span>"</span><span>\t1</span><span>"</span>,<span>0</span>,<span>'</span><span>/</span><span>'</span><span>);
        </span><span>return</span> <span>0</span><span>;
        }
    }</span><span>else</span><span>{
        setcookie(</span><span>'</span><span>refreshTimes</span><span>'</span>,$currentUrl.<span>"</span><span>\t</span><span>"</span>.$_SERVER[<span>'</span><span>REQUEST_TIME</span><span>'</span>].<span>"</span><span>\t1</span><span>"</span>,<span>0</span>,<span>'</span><span>/</span><span>'</span><span>);
        </span><span>return</span> <span>0</span><span>;
    }
}
 
$key </span>= md5($_SERVER[<span>'</span><span>REQUEST_URI</span><span>'</span><span>]);
$path </span>=<span> CACHE_PATH.$key;

</span><span>if</span><span>(is_file($path)
 and filemtime($path)</span>+CACHE_EXPIRE > $_SERVER[<span>'</span><span>REQUEST_TIME</span><span>'</span><span>]
 and refreshTimes() </span>== <span>0</span><span>
 ){
    readfile($path);
    exit();
}

ob_start();
</span>?>

foot.php

<?<span>php

$html </span>=<span> ob_get_contents();
ob_clean();

file_put_contents($path,$html);

echo $html;

</span>?>

test.php

<?<span>php
include </span><span>'</span><span>head.php</span><span>'</span><span>;

$title </span>= <span>"</span><span>Hello world~</span><span>"</span><span>;
</span>?>
<!DOCTYPE html PUBLIC <span>"</span><span>-//W3C//DTD XHTML 1.0 Transitional//EN</span><span>"</span> <span>"</span><span>http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</span><span>"</span>>
<html xmlns=<span>"</span><span>http://www.w3.org/1999/xhtml</span><span>"</span>>
<head>
<meta http-equiv=<span>"</span><span>Content-Type</span><span>"</span> content=<span>"</span><span>text/html; charset=gb2312</span><span>"</span> />
<title><?php echo $title?></title>
</head>

<body>
<?<span>php
echo </span><span>"</span><span>是大方大方</span><span>"</span><span>;
</span>?>
</body>
</html>
<?<span>php
include </span><span>'</span><span>foot.php</span><span>'</span><span>;
</span>?>

 

//其他看到的很全的php文件缓存

看http://www.cnblogs.com/coolid/archive/2012/09/14/2684373.html 链接

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn