In order to improve the speed of the site visit, we often generate a static way to achieve, this really did a lot of speed up the site visits.But the timing generated by some limitations, if you have a separate server can be set on the server plan Task, but if it is using the virtual host may not be easy.Although a lot of ways.But it is easy to use, I think it is still the first judgment of the generated home page file generation time and the difference between the existing time, If you meet a certain value to start generating this method is more easy. No more to say.
Before you begin, mention three functions: "ob_start (), ob_end_clean (), ob_get_contents ()"
ob_start (): is to open the buffer is to cache the contents of the static file you need to generate here;
ob_get_contents (): read the contents of the buffer, the following code example;
ob_end_clean (): This is more important, only the use of this function, the contents of the buffer will be read out;
The following is quoted content:
if (file_exists ("./index.htm")) / / See static index.htm file exists
{
$ time = time ();
// File modification time and the current time difference ?, Directly to htm file, or regenerate htm
if ($ time-filemtime ("./index.htm") <600)
{
header ("Location: classhtml / main.htm");
}
}
// add ob_start () to your beginning;
ob_start ();
// Home 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 the file
$ fp = fopen ("./index.htm", w);
fwrite ($ fp, $ temp) or die (write file error);
// echo "Generate HTML completed!";