Rumah > pembangunan bahagian belakang > tutorial php > 文件缓存,php文件缓存_PHP教程

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

WBOY
Lepaskan: 2016-07-12 08:57:19
asal
1229 orang telah melayarinya

文件缓存,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>?>
Salin selepas log masuk

foot.php

<?<span>php

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

file_put_contents($path,$html);

echo $html;

</span>?>
Salin selepas log masuk

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>?>
Salin selepas log masuk

 

//其他看到的很全的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 ' ]. ' /...
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan