Use php's ob_start to generate static pages for analysis

Source: Internet
Author: User
Php uses ob_start to generate static pages. For more information about generating static pages, see.

Php uses ob_start to generate static pages. For more information about generating static pages, see.

Although there are many methods, it is easy to use. I think we should first determine the difference between the generation time of the generated homepage file and the existing time. If a value is satisfied, the file will be generated, this method is easy to use. Let's get started with it!

Before you start, let's mention three functions: "ob_start (), ob_end_clean (), ob_get_contents ()"

Ob_start (): it is used to open the buffer, that is, to cache the content of the static file you need to generate here;
Ob_get_contents (): Read the content in the buffer. The following code is used as an example;
Ob_end_clean (): This is important. Only when this function is used can the content in the buffer be read. Copy the content to the clipboard code:
The Code is as follows:
If (file_exists ("./index.htm") // check whether the static index.htm file exists.
{
$ Time = time (); // is the file modification time different from the current time? Otherwise, the htm file will be generated again.
If ($ time-filemtime ("./index.htm") <600)
{
Header ("Location: classhtml/main.htm ");}
}

// Add ob_start (); CHINAZ to your start

// The homepage content is your dynamic part

// Add ob_end_clean () to the end and output this page to a variable.
$ Temp = ob_get_contents ();
Ob_end_clean ();

// Write a file
$ Fp = fopen ("./index.htm", 'w ');
Fwrite ($ fp, $ temp) or die ('file writing error ');
// Echo "HTML generation complete! ";


Instance code:
The Code is as follows:
Ob_start ();
?>




<? Php echo 'programmed navigation dh.jb51.net';?>






$ CacheStr = ob_get_contents ();
$ Handle = fopen ("jb51.html", "w ");
Fwrite ($ handle, $ cacheStr );
Ob_clean ();
?>

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.