Heim > Backend-Entwicklung > PHP-Tutorial > 文件缓存,php文件缓存_PHP教程

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

WBOY
Freigeben: 2016-07-12 08:57:19
Original
1240 Leute haben es durchsucht

文件缓存,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>?>
Nach dem Login kopieren

foot.php

<?<span>php

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

file_put_contents($path,$html);

echo $html;

</span>?>
Nach dem Login kopieren

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>?>
Nach dem Login kopieren

 

//其他看到的很全的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 ' ]. ' /...
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage