Principles and Examples of static php pages

Source: Internet
Author: User
Static pages can reduce one server load. if a database connection on the same machine can only load IP addresses, it is estimated that the static htm file can load IP addresses, so many of our sites are static pages, which is also the main reason... static pages can reduce one server load. if a database connection on the same machine can only load IP addresses, it is estimated that the static htm file can load IP addresses, therefore, many of our sites are static pages, which is also the main reason. let's take a look at the principles and examples of static php pages.

In every web project, when the daily PV volume reaches 100,000 or millions or even higher, your website response speed determines whether your project can survive, or is there another technology besides upgrading server configurations, optimizing code, and optimizing databases? Static pages are also the technologies you have to use.

We know that before the page is output, the output data will be temporarily stored in the BUFFER and then output to the page. Therefore, the data in the BUFFER is your PHP code execution, in addition, when the static data generated after the template is rendered, that is, the html page, is not updated quickly or the page information is not highly real-time required, we can write the BUFFER data into an html file and directly request this html page the next time we access it. This saves the PHP code execution time in the middle, the database data query time and template rendering time can greatly improve the page response speed for pages with large amounts of database I/O.

The specific implementation is as follows:

/*** Here is your PHP logic code * // before all the output content on your page, enable the buffer ob_start (); echo 'hello, word! '// When the ob_start () function is used, no output is displayed on the page. at this time, all the output is stored in the cache. // then, we take out the content in the cache, in a static Html file, file_put_contents('index.html ', ob_get_contents (); // function ob_get_contents () indicates that the current buffer content is retrieved; // disable the buffer and clear the buffer content ob_end_clean ();/* in this way, a static html file is generated. then, you only need to determine when to generate an html file, when to access the Html file directly? the following message is displayed: you can control the cache validity period by using the last modification time and current time interval of the filemtime() index.html file: * // Set the expiration date to 120 seconds. $ cacheTime = '000000'; if(time(%-filemtime('index.html ')> $ cacheTime) {// The cache validity period is not valid, regenerate the cache file // Here is your PHP logic code and the code that generates the cache file} else {// directly call the index.html file within the effective period}


Article URL:

Reprint ^ at will, but please attach the tutorial address.

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.