簡單的頁面緩衝技術(二) - 中國WEB開發者網絡 (http://www.webasp.net) -- 技術教程 (http://www.webasp.net/article/) --- 簡單的頁面緩衝技術(二) (http://www.webasp.net/article/16/15110.htm) |
| -- 作者:未知 -- 發佈日期: 2004-11-25 |
| 我的具體實現的例子
為了幫助大家有個感性認識,這裡我給出在我的主頁上實現的基於文件處理的方法。只有主要的處理代碼,不完整。 <? 1 $tmpfile="../tmp/".basename($REQUEST_URI); 2 $tmpfile=str_replace("?", "_", $tmpfile); 3 $tmpfile=str_replace("&", "_", $tmpfile); 4 if(file_exists($tmpfile)) 5 { 6 $cflag=false; 7 $dtmp=filemtime($tmpfile); 8 $itmp=filemtime($incfile); 9 $cflag=$cflag | ($dtmp < $itmp); 10 $ctmp=filemtime(basename($PHP_SELF)); 11 $cflag=$cflag | ($dtmp < $ctmp); 12 $ttmp=filemtime("template/content.ihtml"); 13 $cflag=$cflag | ($dtmp < $ttmp); 14 } 15 else 16 $cflag=true; 17 18 if(!$cflag) //使用存在的文件 19 { 20 readfile($tmpfile); 21 exit; 22 } 23 24 //創建新的文件 25 include "template.class.php3"; 26 27 $fp=fopen($incfile, "r"); 28 $content=fread($fp, filesize($incfile)); 29 fclose($fp); 30 31 //下面進行模版處理 32 $t = new Template("template", "keep"); 33 34 $t->set_file("contentfile","content.ihtml"); 35 36 $t->set_var( 37 array( 38 "content"=>$content 39 )); 40 41 $t->parse("outputcontent","contentfile"); 42 43 $fp=fopen($tmpfile, "w"); 44 if($fp) 45 { 46 flock($fp, 3); 47 fwrite($fp, $t->get_var("outputcontent")); 48 flock($fp, 1); 49 fclose($fp); 50 } 51 $t->p("outputcontent"); ?> 先向大家介紹一下我的目錄結構: /---bin/ 執行程序目錄 | |--content.php3 用於處理文件顯示的程序 | |--template/ 用於存放模板文件的目錄 | |---content.ihtml 模板文件 |-docs/ 數據文件 |-tmp/ 存放緩衝文件 |
| webasp.net |