However, scheduled generation has some limitations. If you have an independent server, you can set up scheduled tasks on the server, but if you use a virtual host, it will be difficult. Although there are many methods, it is 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. Not much more to say. Let’s get started!
I found it online, remember it. Practice has proven that it is available.
Copy code The code is as follows:
$nowtime=time();
$pastsec = $nowtime – $_GET["t"];
if($pastsec<60)
{
exit; //Updated once every minute, the time can be adjusted by yourself
}
ob_start(); //Open the buffer
include("index.php");
$content = ob_get_contents(); //Get the contents of the buffer
$content .= “n”; //Add the code to call the update program
file_put_contents("index.html",$content);
if (!function_exists("file_put_contents"))
{
function file_put_contents($fn,$fs)
{
$fp=fopen($fn,”w+”);
fputs($fp,$fs);
fclose($fp);
}
}
Here are some explanations: Before we start, let’s mention three functions: "ob_start(), ob_end_clean(), ob_get_contents()"
Copy code The code is as follows:
ob_start(): opens the buffer, which is to cache the content of the static file you need to generate here;
ob_get_contents(): Reads the contents of the buffer. The following code is an example;
ob_end_clean(): This is more important. Only after using this function, the contents of the buffer will be read;
[code]
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?, direct to the htm file, otherwise regenerate the htm
if($time-filemtime("./index.htm")< ; 600)
{
header("Location:classhtml/main.htm");
}
}
//Add ob_start( at the beginning of your );
ob_start();
//The content of the homepage 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('Error writing file');
//echo" HTML generation completed! ";
[html]
http://www.bkjia.com/PHPjc/322155.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322155.htmlTechArticleHowever, scheduled generation has some limitations. If you have an independent server, you can set scheduled tasks on the server. But it’s not easy if you use a virtual host. Although the method is very...