文件缓存,php文件缓存_PHP教程

WBOY
發布: 2016-07-12 08:57:19
原創
1217 人瀏覽過

文件缓存,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 链接

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1109846.htmlTechArticle文件缓存,php文件缓存 不多说,看代码 Head.php ? phpdefine( ' CACHE_EXPIRE ' , 1800 ); //设置过期时间define( ' CACHE_PATH ' ,$_SERVER[ ' DOCUMENT_ROOT ' ]. ' /...
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板