Home > php教程 > PHP开发 > PHP automatically generates static HTML implementation code at regular intervals

PHP automatically generates static HTML implementation code at regular intervals

高洛峰
Release: 2016-12-21 11:18:21
Original
1263 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 this method. Not much to say. Let’s get started!

Online If you find it, write it down. Practice has proven that it is available.

<?php 
$nowtime=time(); 
$pastsec = $nowtime – $_GET["t"]; 

if($pastsec<60) 
{ 
exit; //1分钟更新一次,时间可以自己调整 
} 

ob_start(); //打开缓冲区 
include(”index.php”); 
$content = ob_get_contents(); //得到缓冲区的内容 
$content .= “n<script language=javascript src=”f5.php?t=”.$nowtime.”"></script>”; //加上调用更新程序的代码 

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); 
} 
}
Copy after login

Here are some explanations:
Let’s mention three functions before starting: "ob_start(), ob_end_clean(), ob_get_contents()"

ob_start():是打开缓冲区的,就是要把你需要生成的静态文件的内容缓存在这里; 
ob_get_contents():是读出缓冲区里的内容,下面有代码为例; 
ob_end_clean():这个比较重要,只有使用了这个函数后,缓冲区里的内容才会读取出来;
Copy after login

[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 your beginning ;
ob_start();

//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 "Generating HTML completed!";
[html]



For more articles related to the implementation code of PHP automatically generating static HTML at regular intervals, please pay attention to the PHP Chinese website!


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template