Home > Backend Development > PHP Tutorial > Analysis of methods to generate static pages using PHP's ob_start_PHP tutorial

Analysis of methods to generate static pages using PHP's ob_start_PHP tutorial

WBOY
Release: 2016-07-21 15:31:13
Original
1113 people have browsed it

Although there are many methods, they are simple and easy to use. I think it is easier to first judge the difference between the generation time of the generated home page file and the existing time. If a certain value is met, start generating it. Without further ado, let’s get started!

Before we start, let’s mention three functions: "ob_start(), ob_end_clean(), ob_get_contents()"

ob_start(): It opens the buffer, which means you The contents of the static files that need to be generated are cached here;
ob_get_contents(): reads the content in the buffer, the code is below as 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:

Copy code The code is as follows:

if(file_exists("./index.htm"))//Check whether the static index.htm file exists
{
$time=time(); //If the file modification time is different from the current time? Directly direct to htm file, otherwise regenerate htm
if($time-filemtime("./index.htm")< 600)
{
header("Location:classhtml/main.htm") ; }
}

//Add ob_start() at the beginning; CHINAZ

//The 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 to file
$fp= fopen("./index.htm",'w');
fwrite($fp,$temp) or die('Write file error');
//echo "Generating HTML completed!";


Example code:
Copy code The code is as follows:

ob_start();
?>




<?php echo 'Programming Navigation dh.jb51.net';?>






$cacheStr=ob_get_contents();
$handle=fopen("jb51.html","w");
fwrite($ handle, $cacheStr);
ob_clean();
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323068.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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template