PHP summarizes the method of generating HTML pure static pages for the entire website

  • 2020-05-12 02:20:47
  • OfStack

 
<?php 
// Join where you started  ob_start(); 
ob_start(); 

// Here's your code  
// Add at the end  ob_end_clean(), And output this page to 1 A variable  
$temp = ob_get_contents(); 
ob_end_clean(); 

// Written to the file  
$fp = fopen( 'the name of the file ','w'); 
fwrite($fp,$temp) or die( 'writing file error '); 
?> 


This is only the most basic method, and it is not very practical, because the website is updated and HTML is periodically regenerated

Here's how I did it:
 
if(file_exists( " xxx.html " )) 
{ 
$time = time(); 

// The file modification time is half an hour different from the current time 1 Next, direct direction html File, otherwise regenerate html 
if($time - filemtime( " xxx.html " ) < 30*60) 
{ 
header( " Location:xxx.html " ); 
} 
} 
// Join where you started  ob_start(); 
ob_start(); 

// Details of the page  
// Add at the end  ob_end_clean(), And output this page to 1 A variable  
$temp = ob_get_contents(); 
ob_end_clean(); 

// Written to the file  
$fp = fopen( ' xxx.html','w'); 
fwrite($fp,$temp) or die( 'writing file error '); 

// redirect  
header( " Location:xxx.html " ); 

Here is an introduction to some of the functions used:
1. Flush: flush the contents of the buffer and output.
Function format: flush()
Note: this function is often used, high efficiency.

2. ob_start: open the output buffer
Function format: void ob_start(void)
Note: when the buffer is activated, all non-file header information from the PHP program is not sent, but is stored in the internal buffer. 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)
Note: this function returns the contents of the current buffer and FALSE if the output buffer is not active.
4, ob_get_length: returns the length of the internal buffer.
Usage: int ob_get_length(void)
Description: this function will return the length of the current buffer; And ob_get_contents1 if the output buffer is not activated. Returns FALSE.
5. ob_end_flush: sends the contents of the internal buffer to the browser and closes the output buffer.
Usage: void ob_end_flush(void)
Note: 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)
Note: this function does not output the contents of the internal buffer but deletes it!
7, ob_implicit_flush: on or off absolute refresh
Usage: 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 1. By default, the buffer is closed. After the absolute output is turned on, each script output is sent directly to the browser, and flush() is no longer called.

Related articles: