Copy the code The code is as follows:
//Add ob_start() at the beginning;
ob_start();
//The following is your code
//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('file name','w');
fwrite( $fp,$temp) or die('Writing file error');
?>
This is just the most basic method, not very practical, because the website needs to be updated and the HTML needs to be regenerated regularly
The following is The method I use:
Copy the code The code is as follows:
if(file_exists("xxx.html"))
{
$time = time();
//The difference between the file modification time and the current time If it takes half an hour, it will be directed to the html file, otherwise the html will be regenerated
if($time - filemtime("xxx.html") < 30*60)
{
header("Location:xxx.html");
}
}
//Add ob_start() at the beginning;
ob_start();
//Details of the page
//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('xxx.html','w');
fwrite($fp,$temp) or die('Write file error') ;
//Redirect
header("Location:xxx.html");
The following is an introduction to some functions used:
1. Flush: Refresh the contents of the buffer and output.
Function format: flush()
Description: This function is frequently used and is very efficient.
2. ob_start: Open the output buffer
Function format: void ob_start(void)
Description: When the buffer is activated, all non-file header information from the PHP program will not be sent, but will be saved in the internal buffer. In order to output the contents of the buffer, you can use ob_end_flush() or flush() to output the contents of the buffer.
3, ob_get_contents: Return the contents of the internal buffer.
Usage: string ob_get_contents(void)
Description: This function will return the contents of the current buffer. If the output buffer is not activated, it will return FALSE.
4. ob_get_length: Returns the length of the internal buffer.
Usage: int ob_get_length(void)
Description: This function will return the length in the current buffer; like ob_get_contents, if the output buffer is not activated. then returns FALSE.
5. ob_end_flush: Send the contents of the internal buffer to the browser and close the output buffer.
Usage: void ob_end_flush(void)
Description: This function sends the contents of the output buffer (if any).
6. ob_end_clean: Delete the contents of the internal buffer and close the internal buffer.
Usage: void ob_end_clean(void)
Explanation: This function will not output the contents of the internal buffer but delete it!
7. ob_implicit_flush: Turn on or off absolute flush
Usage method: void ob_implicit_flush ([int flag])
Note: Anyone who has used Perl knows the meaning of $|=x. This string can open/close the buffer, and The ob_implicit_flush function is the same as that one. The buffer is turned off by default. After turning on absolute output, each script output is sent directly to the browser, and there is no need to call flush().
The above has introduced a summary of tmal PHP's method of generating pure static HTML web pages from the entire website, including tmal content. I hope it will be helpful to friends who are interested in PHP tutorials.