(PHP Learning Notes) for page statics

Source: Internet
Author: User
Tags dsn php template

When we visit each portal information website, the homepage will show us all kinds of headlines, navigation information (such as news Network). We can imagine that, in the face of numerous database access operations, loading speed will become slow when we visit these pages. As we all know, the speed of our access to static pages is better than dynamic Web pages. Therefore, the implementation of page static can effectively solve these problems.

So, what is static? In fact, according to the literal understanding, we can learn that static is to transform a dynamic Web page into a static-like Web page. The essence of dynamic page static is to generate a static HTML file.

Static requires two key points of knowledge:

①buffer buffer mechanism

② writing files using function file_put_contents ()

The process of static:

1, PHP template page (the same as the HTML page to be displayed, the difference is that the PHP code is added)

2. Write to static file and generate HTML file (extract template file from buffer)


First, we'll start by writing an action class for manipulating templates and generating static HTML files:


operation.php:

$dsn = ' mysql:dbname=chatroom;host=127.0.0.1 '; $user = ' root '; $password = ' 123456 '; $pdo = new PDO ($DSN, $user, $password); $sql = "SELECT * FROM Messages limit 3"; $stmt = $pdo->prepare ($sql); $result = $stmt->execute (); $stmt  Setfetchmode (PDO::FETCH_ASSOC); Returns the result in the form of an associative array $rows = $stmt->fetchall (); Ob_start (); Open buffer require_once (' muban.php ');

Code interpretation: first read the data from the database, and then introduce the ' muban.php ' file, in order to let it in the introduction of the process into the buffer.


According to the process steps:

1. PHP Template page

For demonstration convenience, simply write an interface as follows:

muban.php

<body> <div id= "wrap" > 


Code interpretation: Traverse the ' $rows ' array in ' operation.php ' file and output the result to the <li> tag.

2. Write to static file and generate HTML file (extract template file from buffer)

Follow the following code in operation.php:

if (file_put_contents (' index.html ', Ob_get_clean ())) {echo "Success";  }else{echo "false"; }

The ' file_put_contents () ' method is used to write the contents of the buffer to the ' index.html ' file, and the static file generation is completed. The Ob_get_clean () method reads the data from the buffer and clears the buffer (the purge buffer can also be understood as loading the contents of this function is not displayed in the terminal)


Finally, the ' index.html ' interface is generated:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/85/F8/wKiom1exN96QY5qKAABa6SBTmlc450.png "title=" Qq11.png "alt=" Wkiom1exn96qy5qkaaba6sbtmlc450.png "/>


From the console you can see that the load response time is:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/85/F7/wKioL1exN_Ch0PjmAAAwTnacHAw132.png "title=" Qq1.png "alt=" Wkiol1exn_ch0pjmaaawtnachaw132.png "/>

Let's look at the muban.php page again:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/85/F7/wKioL1exNwmDbtOEAABeWhA22l4225.png "title=" Qq20160815112852.png "alt=" Wkiol1exnwmdbtoeaabewha22l4225.png "/>

It was said that the generated HTML static files are generated from the template interface, so the interface is the same. Then look at the corresponding time of the load:

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/85/F7/wKioL1exN1mQMl4ZAAA0UXTLQ_4287.png "title=" Qq20160815113022.png "alt=" Wkiol1exn1mqml4zaaa0uxtlq_4287.png "/>

From the response time we can see that only a few lines of code has created such a big gap. It is conceivable that when the amount of information on the page is very large, the speed of loading response is very large. The use of page static, which can greatly avoid such problems arise.


-----------------------------------------------------------------------------------------

When it comes to generating static files, what if the page information changes? The answer is to regenerate (is this nonsense?). )

The automatic triggering system generates static pages in three common ways:

1, the page to add cache time. When a user requests a page, the background determines whether the page time expires (exceeding the set cache time), generates a new static page if it is exceeded, or gets a static page.

2, manually trigger the generation. In order to solve the problem of the page modification but not timely display, add "one button Update" button in the background to generate a static file after clicking.

3, crontab timed scanning program. This approach involves applications on the Linux operating system:

*/5**** php/data/staticpage/operation.php

This statement means that the following index.php files are executed every 5 minutes. Because I do not know deeply about it, do not do too much to describe, lest fraught.

This article is from the "W1SW" blog, make sure to keep this source http://cfdeng.blog.51cto.com/9873532/1838077

(PHP Learning Notes) for page statics

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.