Home  >  Article  >  Backend Development  >  How to use php's ob_start to generate static pages_PHP tutorial

How to use php's ob_start to generate static pages_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:15844browse

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;
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:

if(file_exists("./index.htm"))//Check whether the static index.htm file exists
{
$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 "); }
}

//Add ob_start();CHINAZ

at your beginning

//Homepage content is your dynamic part

//Add ob_end_clean() at the end and output this page to a variable
$temp=ob_get_contents();
ob_end_clean();

//Write file
$fp=fopen("./index.htm",w);
fwrite($fp,$temp) or die(write file error);
/ /echo "Generating HTML completed!";

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508243.htmlTechArticleAlthough there are many methods, they are simple and easy to use. I think it is better to first determine the generation time of the generated homepage file. The difference between the current time and the current time, if a certain value is met, start...
Statement:
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