Master several common static page methods and master several static pages. Master several common static page methods, Master several static pages, commonly referred to as static pages, divided into two types, one is pseudo static, that is, url rewriting, one is true and static. We can master several common static page methods and master several static pages.
The static page can be divided into two types: pseudo-static, namely url rewriting, and real static. Let's talk about it with true and static.
What is PHP static
A simple understanding of PHP static is to make website pages displayed in the form of static HTML in front of visitors. PHP static is divided into pure static and pseudo static, the difference between the two is that the processing mechanism for generating static pages in PHP is different.
Why static web pages?
1. speed up page opening and browsing. static pages do not need to be connected to databases. the speed of opening is significantly higher than that of dynamic pages;
2. it is conducive to SEO optimization by search engines. both Baidu and Google will give priority to static pages, not only quick but also full;
3. reduce the burden on the server. you do not need to call the system database to browse the webpage;
4. websites are more secure, and HTML pages are not affected by php-related vulnerabilities. viewing a large website is basically static pages, which can reduce attacks and prevent SQL injection.
When a database error occurs, normal website access is not affected.
Although generating html articles is difficult to operate and complicated in the program, these sacrifices are worth the effort to make it easier for search and make it faster and safer.
PHP static HTML page generation method
Use PHP templates to generate static pages
Static PHP templates are very convenient. for example, if you install and use PHP Smarty to achieve static website, you can also write a set of template parsing rules yourself. Common templates can imitate various cms template rules.
1. use the php file read/write function and ob Cache mechanism to generate static pages
For example, the dynamic details page address of a product is: http://xxx.com? Goods. php? Gid = 112
So here we read the content of this Details page based on this address and save it as a static page. next time someone accesses the dynamic address of this product details page, we can
Directly output the generated static content file.
Time () {// If echo file_get_contents ($ goods_statis_file) is not expired; // output static file content exit;} else {// If unlink ($ goods_statis_file) has expired ); // delete the expired static page file ob_start (); // read data from the database and assign the value to the relevant variable // include ("xxx.html "); // load the corresponding product details page template $ content = ob_get_contents (); // assign the details page content to the $ content variable file_put_contents ($ goods_statis_file, $ content ); // write the content to the corresponding static file ob_end_flush (); // output the product details page information} else {ob_start (); // read data from the database, and assign the value to the relevant variable // include ("xxx.html" ); // Load the corresponding product details page template $ content = ob_get_contents (); // assign the details page content to the $ content variable file_put_contents ($ goods_statis_file, $ content ); // write the content to the corresponding static file ob_end_flush (); // output the product details page information}?>
2. use nosql to read content from memory (in fact, this is not static but cached );
Take memcache as an example:
Connect ('memcache _ host', 11211); $ mem_goods_content = $ mem-> get ($ goods_statis_content); if ($ mem_goods_content) {echo $ mem_goods_content ;} else {ob_start (); // read data from the database and assign values to related variables // include ("xxx.html "); // load the corresponding product details page template $ content = ob_get_contents (); // assign the details page content to $ content variable $ mem-> add ($ goods_statis_content, $ content, false, $ expr); ob_end_flush (); // output product details page information}?>
Memcached is a one-to-one correspondence of key values. the default key size cannot exceed 128 bytes, and the default value size is 1 M. Therefore, the size of 1 M meets the requirements of storage for most webpages.
The above is the method for static pages. I hope it will be helpful to my friends.
Update wonderful technical articles every day at www.phpskill.com.
Php pure technology learning exchange group: 323899029
From: http://www.phpskill.com/html/show-1-4418-1.html
Usually, static pages are divided into two types: pseudo-static, namely url rewriting, and real static. We use...