PHP periodically automatically generates static HTML implementation code

  • 2020-03-31 20:53:40
  • OfStack

But timing generated by some limitations, if everyone can have independent server on the server setup planning tasks, but if it is using the virtual host can be tricky. Although the method is very much. But simple and easy to use, I feel or to judge the generated home page file generated first time and the difference between the current time, if you meet some value began to generate this kind of method is easy. Not much said. Get started!

I found it online. Make a note of it. Practice proves that it can be used.
 
<?php 
$nowtime=time(); 
$pastsec = $nowtime  �  $_GET["t"]; 

if($pastsec<60) 
{ 
exit; //Update every 1 minute, the time can be adjusted
} 

ob_start(); //Open buffer
include( " index.php " ); 
$content = ob_get_contents(); //Gets the contents of the buffer
$content .=  " n<script language=javascript src= " f5.php?t= " .$nowtime. " "></script> " ; //Plus the code that calls the updater

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:
Let's mention three functions before we start :"ob_start(), ob_end_clean(), ob_get_contents()"
 
ob_start(): Is to open the buffer, which is to cache the contents of the static file you need to generate here;  
ob_get_contents(): Is to read the contents of the buffer, the following code as an example;  
ob_end_clean(): This is important because the contents of the buffer will not be read until this function is used.  


[code]
If (file_exists("./index.htm"))// see if the static index.htm file exists
{
$time = time ();

// the difference between the file modification time and the current time? Direct to the HTM file, otherwise rebuild HTM
If ($time - filemtime (". / index. HTM ") < 600).
{
The header (" Location: classhtml/main. HTM ");

}
}

// add ob_start() to your start;
Ob_start ();

// home page content, is your dynamic part

// add ob_end_clean() at the end and output the 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" HTML is generated!" ;
[HTML]

Related articles: