Home > Backend Development > PHP Tutorial > Summary of PHP methods to generate HTML pure static web pages for the entire website_PHP Tutorial

Summary of PHP methods to generate HTML pure static web pages for the entire website_PHP Tutorial

WBOY
Release: 2016-07-21 15:20:56
Original
759 people have browsed it

Copy code The code is as follows:

//Add ob_start();
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 file
$fp = fopen('file name','w');
fwrite($fp,$temp) or die(' Error writing file');
?>


This is only the most basic method and is 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 code The code is as follows:

if(file_exists(“xxx. html”))
{
$time = time();

//If the file modification time is less than half an hour different from the current time, direct it to the html file, otherwise regenerate the html
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. Instead, it is saved in an 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: Returns 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; the same as 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 method: void ob_end_clean(void)
Description: This function will not output the contents of the internal buffer but delete it !
7. ob_implicit_flush: Turn on or off absolute refresh
Usage method: void ob_implicit_flush ([int flag])
Note: Anyone who has used Perl knows the meaning of $|=x, this string can be turned on /Close the buffer, and the ob_implicit_flush function is the same as that. The default is to close the buffer. After turning on absolute output, each script output is sent directly to the browser, and there is no need to call flush().

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325004.htmlTechArticleCopy the code as follows: ?php //Add ob_start(); ob_start(); / /The following is your code//Add ob_end_clean() at the end and output this page to a variable $te...
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