开通黄钻
![](http://qzs.qzone.qq.com/qzone/space_item/orig/9/81177_dec5.jpg)
<?php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| /*
**此段代码要放在所有页面都包含的模块里
**比如const.php或conn.php
**$isHTMLModel 此变量设置是否保存页面内容为静态文件
**$spacing 此变量设置更新时间
**$HtmlPath 此变量保存当前显示的页面的静态文件的路径
**UseHtmlPage 此函数的作用主要是判断当前页面的HTML静态文件是否过期,是则返回文件路径
*/
$isHTMLModel =0; //是否使用静态化页面
$spacing =1; //生成新的文件更新间隔时间 单位:小时
$HtmlPath =UseHtmlPage( $isHTMLModel , $spacing );
/*
**此段代码放在需要输出静态文件的页面最底部
**主要作用就是根据上面的设置参数,把当前动态页面里显示过的内容
**生成为静态HTML文件,保存的静态文件的路径在$HtmlPath里
*/
CreateHtmlPage( $HtmlPath , $isHTMLModel )
| ?>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| //如果使用静态页面则取得当前页面的路径,并判断是否存在静态文件和文件的生成日期时间戳与当前日期时间戳是否相差$spacing这么多,有则跳转,无有则生成静态文件路径
function UseHtmlPage( $isHTMLModel , $spacing ){
if ( $isHTMLModel ){
$paramet = "" ;
$paramet = $_SERVER [ 'QUERY_STRING' ];
if ( $paramet != "" ) $paramet = "=" . str_replace ( "=" , "-" , str_replace ( "&" , "_" , $_SERVER [ 'QUERY_STRING' ])). "" ;
$HtmlPath =dirname( $_SERVER [ "SCRIPT_FILENAME" ]). "\\html" . str_replace ( ".php" , $paramet . ".html" , str_replace ( "/" , "\\" , $_SERVER [ "PHP_SELF" ]));
$Path = "/html" . str_replace ( ".php" , $paramet . ".html" , $_SERVER [ "PHP_SELF" ]);
if ( file_exists ( $HtmlPath )){
$one = strtotime ( date ( 'r' , filemtime ( $HtmlPath ))); //文件之前 时间戳
$two =time(); //系统现在 时间戳
$cle = $two - $one ; //得出时间戳差值
if ( $H < $spacing ) {header( "Location: " . $Path );}
}
return $HtmlPath ;
}
}
//如果使用静态页面并且有静态文件路径,则把当前页面的缓存写到静态文件中
function CreateHtmlPage( $HtmlPath ,
$isHTMLModel ){
if ( $isHTMLModel ) WriteHtml( $HtmlPath );
}
//把当前页面的缓存写到静态文件中
function WriteHtml( $filepath ){
if ( $filepath != "" ){
$content =ob_get_contents(); //取得php页面输出的全部内容
ob_flush();
@unlink( $filepath );
$fp = fopen ( $filepath , "w" ); //创建一个文件,并打开,准备写入
fwrite( $fp , $content ); //把php页面的内容全部写入
fclose( $fp );
}
}
|
|
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理