PHP generates HTML static throughout the site

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags code dynamic website file function how to html internal internet

I often see someone on the Internet asking how to static the entire dynamic website, in fact, the method is very simple. 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

I often see someone on the Internet asking how to static the entire dynamic website, in fact, the method is very simple.


<? php
// add ob_start () to your beginning;
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 the file
$ fp = fopen ('filename', 'w');
fwrite ($ fp, $ temp) or die ('write file error');
?>

This is only the most basic method, not very practical, because the site is to be updated, to regularly regenerate HTML

Here's how I use it:

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

// Change the time between the file and the current time a half hour about, direct html file, or re-generate html
if ($ time - filemtime ("xxx.html") <30 * 60)
{
header ("Location: xxx.html");
}
}
// add ob_start () to your beginning;
ob_start ();

// page details
// 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 ('xxx.html', 'w');
fwrite ($ fp, $ temp) or die ('write file error');

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


The following is a description of some of the functions used:
1, Flush: refresh the contents of the buffer, the output.
Function Format: flush ()
Description: This function is often used, high efficiency.
2, ob_start: open the output buffer function Format: void ob_start (void)
Explanation: 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.
Use: string ob_get_contents (void)
Description: This function returns the contents of the current buffer, or FALSE if the output buffer is not active.
4, ob_get_length: return the length of the internal buffer.
Use: int ob_get_length (void)
Description: This function returns the length of the current buffer; same as ob_get_contents if the output buffer is not active. FALSE is returned.
5, ob_end_flush: send the contents of the internal buffer to the browser, and close the output buffer.
Use: 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 Use: void ob_end_clean (void)
Description: This function does not output the contents of the internal buffer but delete it!
7, ob_implicit_flush: turn on or off the absolute refresh Use: void ob_implicit_flush ([int flag])
Description: Perl people know the meaning of $ | = x, the string can be opened / closed the buffer, and ob_implicit_flush function and that, the default is to turn off the buffer, open the absolute output, each script output Sent directly to the browser, no longer need to call 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.