PHP generates HTML static for the entire Web site

Source: Internet
Author: User
Tags flush header variable
I often see people on the Internet to ask how the entire dynamic Web site static, in fact, the implementation of the method is very simple.

<?php
Add Ob_start () at the beginning of your service;
Ob_start ();

Here's your code.
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 (' 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-guide
Header ("Location:xxx.html");



Here is an introduction to some of the functions used:
1, Flush: Refresh the contents of the buffer, output.
function format: Flush ()
Description: This function is frequently used and highly efficient.
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 saved 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: Return the contents of the internal buffer.
Using method: String ob_get_contents (void)
Description: 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.
How to use: int ob_get_length (void)
Description: This function returns the length of the current buffer, as ob_get_contents if the output buffer is not active. Returns FALSE.
5, Ob_end_flush: Send the contents of the internal buffer to the browser, and turn off the output buffer.
Usage method: 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 does not output the contents of the internal buffer but deletes it!
7. Ob_implicit_flush: Turn absolute refresh on or off
How to use: 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 absolute output, each script output is sent directly to the browser, no longer need to call the flush ( )




Related Article

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.