http://67.220.92.14/forum/inde PHP automatically generates static HTML implementation code at regular intervals

WBOY
Release: 2016-07-29 08:43:03
Original
1398 people have browsed it

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 homepage file and the existing time. If a certain value is met, start generating it. Not much to say. Let’s get started!
Found online , remember it. Practice has proven that it is available.

Copy the 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 buffer The content
$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:
Let’s mention three functions before starting: "ob_start(), ob_end_clean(), ob_get_contents()"

Copy the 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 To read out 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 out;


[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 restart Generate htm
if($time-filemtime("./index.htm")< 600)
{
header("Location:classhtml/main.htm");
}
}
//At your beginning Add ob_start();
ob_start();
//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 file
$fp=fopen("./index.htm",'w');
fwrite($fp,$temp) or die('Write file error');
/ /echo" HTML generation completed! ";
[html]

The above introduces the implementation code of http://67.220.92.14/forum/inde PHP automatically generates static HTML at regular intervals, including the content of http://67.220.92.14/forum/inde. I hope that friends who are interested in PHP tutorials Helps.

Related labels:
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