PHP缓存页面方法

WBOY
Release: 2016-06-20 13:03:42
Original
1237 people have browsed it

PHP缓存页面函数。

<p>/*****************************************************************</p>-- 函数名:cache_page(包括cache_page_go)<br />-- 作  用:轻松快速缓存全站<br />-- 参  数:缓存时间(单位:秒)<br />-- 返回值:输出内容<br />-- 实  例:cache_page(300);  函数使用在页面的最上方<br />*******************************************************************/<br /><br />function cache_page($refresh=20){<br />	ob_start();//开启缓冲区<br />	$temp=sha1($_SERVER['PHP_SELF'].'|G|'.serialize($_GET).'|P|'.serialize($_POST));//缓存文件名字<br />	$temp=dirname(__FILE__).'/cache/'.$temp;//缓存文件路径<br />	if(!file_exists($temp)){//缓存文件不存在<br />		register_shutdown_function('cache_page_go',$temp);<br />	}else{//缓存文件存在<br />		if((time()-filemtime($temp))>$refresh ){//缓存超时<br />			register_shutdown_function('cache_page_go',$temp);//调用函数<br />		}else{//正常使用缓存文件<br />			$temp=file_get_contents($temp);//取出缓存文件内容<br />			echo $temp;//输出缓存内容<br />			$temp=ob_get_contents();//取出缓冲区内容<br />			ob_get_clean();    //清空缓冲区<br />			echo $temp;      //输出<br />			unset($temp,$refresh);/*注销变量*/<br />			exit();<br />		}<br />	}<br /><p>}</p><p><br /></p>function cache_page_go($file){<br />	$output=ob_get_contents();//获取缓冲区内容<br />	ob_get_clean();           //清空缓冲区<br />	file_put_contents($file,$output,LOCK_EX);//写入缓存文件<br />	echo $output;//输出缓存内容<br />	unset($output,$file);/*注销变量*/<br />	exit();<br /><p>}</p>
Copy after login

建议将该函数放置在页面的最开始处


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!