「PHPの学習」phpで現在表示した動態PHPを静止化させる、静的htmlをサーバへ生成するサンプルプログラム

PHPコード:

1.呼び出す関数を宣言
$isHTMLModel=0;
$spacing=1;
$HtmlPath=UseHtmlPage($isHTMLModel,$spacing);
CreateHtmlPage($HtmlPath , $isHTMLModel)
2.関数を定義
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);
}
}

Development

Posted by arkgame