程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 文件緩存,php文件緩存

文件緩存,php文件緩存

編輯:關於PHP編程

文件緩存,php文件緩存


不多說,看代碼

Head.php

<?php
define('CACHE_EXPIRE',1800);  //設置過期時間
define('CACHE_PATH',$_SERVER['DOCUMENT_ROOT'].'/cache/'); //設置緩存目錄
define('REFRESH_EXPIRE',10);  
is_dir(CACHE_PATH) or mkdir(CACHE_PATH,0777);

//該方法是獲得用戶的刷新次數,如果用戶連續兩次刷新,那麼就顯示最新內容 function refreshTimes(){ $currentUrl='http://'.$_SERVER["HTTP_HOST"].$_SERVER['REQUEST_URI']; if(isset($_COOKIE['refreshTimes'])){ $cookArray=explode("\t",$_COOKIE['refreshTimes']); if(isset($cookArray[1]) and $_SERVER['REQUEST_TIME'] - (int)$cookArray[1] < REFRESH_EXPIRE and isset($cookArray[0]) and $cookArray[0] == $currentUrl){ setcookie('refreshTimes',$currentUrl."\t".$_SERVER['REQUEST_TIME']."\t".(string)((int)$cookArray[2]+1),0,'/'); return (int)$cookArray[2]; }else{ setcookie('refreshTimes',$currentUrl."\t".$_SERVER['REQUEST_TIME']."\t1",0,'/'); return 0; } }else{ setcookie('refreshTimes',$currentUrl."\t".$_SERVER['REQUEST_TIME']."\t1",0,'/'); return 0; } } $key = md5($_SERVER['REQUEST_URI']); $path = CACHE_PATH.$key; if(is_file($path) and filemtime($path)+CACHE_EXPIRE > $_SERVER['REQUEST_TIME'] and refreshTimes() == 0 ){ readfile($path); exit(); } ob_start(); ?>

foot.php

<?php

$html = ob_get_contents();
ob_clean();

file_put_contents($path,$html);

echo $html;

?>

test.php

<?php
include 'head.php';

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

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

 

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

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved