PHP generates HTML static for the entire Web site

Source: Internet
Author: User
Tags flush

In fact, the implementation of the method is very simple.


php; " >
<?php 
//Join Ob_start () at the beginning of your service; 
Ob_start (); 

Here 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 (' filename ', ' w '); 
Fwrite ($FP, $temp) or Die (' Write file error '); 
? >


This is just the basic method, not very practical, because the site is to be updated, to periodically regenerate the HTML

Here are the methods I used:

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

File modification time and now time difference half an hour, directly to the HTML file, otherwise regenerate HTML
if ($time-filemtime ("xxx.html") < 30*60)
{
Header ("Location:xxx.html");
}
}
Add Ob_start () at the beginning of your service;
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 to File
$fp = fopen (' xxx.html ', ' W ');
Fwrite ($FP, $temp) or Die (' Write file error ');

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


The following are descriptions of the functions used:
1, Flush: Refreshing the contents of the buffer, output. &NBSP
Function Format: Flush ()  
Description: This function is frequently used and highly efficient. &NBSP
2, Ob_start: Open output buffer  
function format: void Ob_start (void)  
Description: When the buffer is activated, all non-file header information from the PHP program is not sent. Instead, it is saved in an internal buffer. To output the contents of the buffer, you can use Ob_end_flush () or flush () to output the contents of the buffer. &NBSP
3, ob_get_contents: Returns the contents of the internal buffer. &NBSP
Use Method: String ob_get_contents (void)  
Description: This function returns the contents of the current buffer, FALSE if the output buffer is not active. &NBSP
4, Ob_get_length: Returns the length of the internal buffer. &NBSP
Use method: int ob_get_length (void)  
Description: This function returns the length in the current buffer, as in ob_get_contents if the output buffer is not active. Returns FALSE. &NBSP
5, Ob_end_flush: Sends the contents of the internal buffer to the browser, and closes the output buffer. &NBSP
Use method: void Ob_end_flush (void)  
Description: This function sends the contents of the output buffer (if any). &NBSP
6, Ob_end_clean: Deletes the contents of the internal buffer and closes the internal buffer  
using the method: void Ob_end_clean (void)  
Description: This function does not output the contents of the internal buffer but deletes it! &NBSP
7, Ob_implicit_flush: Turn absolute refresh on or off  
Use method: void Ob_implicit_flush ([int flag])  
Description: People who have used Perl know the meaning of $|=x, this string can open/close the buffer, and the Ob_implicit_flush function is the same as that, the default is to close the buffer, open the absolute output, each script output directly sent to the bangsThe flush ()

is no longer needed to invoke the navigator.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.