Home > Article > Backend Development > How to use php's ob_start to generate static pages_PHP tutorial
Although there are many methods, they are simple and easy to use. I think it is better to first determine the difference between the generation time and the existing time of the generated homepage file. If a certain value is met, the generation will start. This method is easier to come by, so I won’t say more, let’s get started! Before we start, let’s mention three functions: "ob_start(), ob_end_clean(), ob_get_contents()" ob_start(): is to open the buffer, which is to cache the content of the static file you need to generate here; if(file_exists("./index.htm"))//Check whether the static index.htm file exists //Add ob_start();CHINAZ //Homepage content is your dynamic part //Add ob_end_clean() at the end and output this page to a variable //Write file
ob_get_contents(): is to read the content in the buffer, the following code is an example;
ob_end_clean(): This is more important. Only after using this function, the content in the buffer will be read; copy the content to the clipboard code:
{
$time=time(); //What is the difference between the file modification time and the current time? If so, direct to the htm file, otherwise regenerate htm
if($time-filemtime("./index.htm")< 600)
{
header("Location:classhtml/main.htm "); }
}
$temp=ob_get_contents();
ob_end_clean();
$fp=fopen("./index.htm",w);
fwrite($fp,$temp) or die(write file error);
/ /echo "Generating HTML completed!";