Generating static pages using the OB function of PHP

Source: Internet
Author: User

Before using some open-source CMS management system, it was very curious how the background generated HTML static files are implemented. Today and colleagues discussed the next, did not expect colleagues to do this kind of static page generation function, decisively to him to consult the next.

After he explained, just know that actually generate static HTML page is very simple. PHP provides a special function to implement.

Here's how to do it (implementing generating HTML should be just one way):

Implemented via the OB cache of PHP
Increase speed
1, through the PHP OB cache to achieve
Using the PHP OB cache for page statics
Modifying the php.ini configuration file
Output_buffering=off
1, Cache: Smarty Cache, thinkphp framework cache, PHP OB cache
(1) OB---cache, first the output of the data, buffer to a space
And then display the cached data for that space.
1,ob_start () First place the output data into the OB cache
2,ob_clean () empties the cache but does not close
3,ob_end_clean () empty the cache and also close the cache
4,ob_flush () outputs the OB cache data to the program cache
5,ob_end_flush () outputs the OB cache data to the program cache and closes the OB cache
(1) Program cache
That is, if the OB cache is not turned on, the data is cached in the program, and the Echo is finished, and the output is unified.
Browsers also have caches:
The browser first save data, and so on to a certain amount (ie 500 MB) when the output

The code is as follows:

1<?PHP2     Header(' content-type:text/html; Charset=utf-8 ');3     /**4 Ob_start (): is to open the buffer, is to put the static file you need to generate the contents of the cache here;5 ob_get_contents (): is to read out the contents of the buffer, the following code for example;6 Ob_end_clean (): This is more important, only after using this function, the contents of the buffer will be read out7         8     */9     if(file_exists('./index.html '))//whether a static index.html file existsTen     { One         $time= Time();//file modification time and current time difference, direct navigation to HTML file, otherwise regenerate HTM A          -         if($time-Filemtime('./index.html ') < 60) -         { the             Header(' location:./index.html '); -         } -     } -      +     //add Ob_start () at the beginning; -     Ob_start();//turns on the OB cache for PHP, which is placed in the OB cache without opening the OB cache and then in the program cache +      A //Dynamic part content at?> - - -<meta charset= "Utf-8"/> -<title>Hello</title> -<style> inbody{background:black;color:white;font-size:20px;} -</style> to +<body> -<p align= "center" >php Generate HTML file ....</p> the</body> * $<?PHPPanax Notoginseng     //add Ob_end_clean () at the end and output this page to a variable -     $htmlStr=ob_get_contents();//get the data in the cache the     Ob_end_clean(); +      A     //Write File the     $fp=fopen('./index.html ', ' W '); +     fwrite($fp,$htmlStr) or die(' Write file error '); -      $     Echo"Generate HTML complete!";

Generating static pages using the OB function of PHP

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.