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

文件缓存,php文件缓存

WBOY
풀어 주다: 2016-06-13 08:44:33
원래의
1580명이 탐색했습니다.

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

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿