Master several common methods of page static

Source: Internet
Author: User
Tags php template
Often said page static is divided into two kinds, one is pseudo-static, that is, url rewriting, one is true static. We take the real static as the main point of speaking.

What is PHP static

PHP static simple understanding is to make the site Generation page in the form of static HTML in front of the visitors, PHP static pure static and pseudo-static, the difference is that PHP generated static page processing mechanism is different.


Why make Web pages static
First, speed up the page to open the browsing speed, static pages do not need to connect the database open faster than the dynamic page has significantly improved;
Second, conducive to search engine optimization Seo,baidu, Google will first include static pages, not only is included in the fast also included in the whole;
Third, reduce the burden on the server, browse the Web without calling the system database;
Four, the website is more secure, HTML page will not be affected by PHP related vulnerability; Looking at a larger site is basically a static page, and can reduce attacks, anti-SQL injection.
When a database error occurs, it does not affect normal site access.
Generate HTML article Although the operation of the trouble, the program is complicated, but in order to better search, for faster, more secure, these sacrifices are worth.


How PHP generates static HTML pages

Generate static pages with PHP templates

PHP template Implementation of the static is very convenient, such as the installation and use of PHP smarty to achieve static site, you can also write a set of template parsing rules, common can imitate various types of CMS template rules.


1. Generate a static page using the PHP file read-write function and the OB cache mechanism
For example, the dynamic Details page address for a product is: http://xxx.com?goods.php?gid=112
So here we read the contents of this detail page from this address once, and then save as static page, the next time someone accesses this product Details page dynamic address, we can
Output the corresponding static content file that has been generated directly.

  Time()){//If not expired         Echo file_get_contents($goods _statis_file);//output static file contents         Exit; }Else{//if it has expired            unlink($goods _statis_file);//delete an outdated static page file            Ob_start(); //read the data from the database and assign the value to the relevant variable//include ("xxx.html");//load the corresponding Product detail page template            $content=ob_get_contents();//Assign the details page content to the $content variable            file_put_contents($goods _statis_file,$content);//write content to the corresponding static file            Ob_end_flush();//Export Product Details page information      }}Else{  Ob_start(); //read the data from the database and assign the value to the relevant variable//include ("xxx.html");//load the corresponding Product detail page template  $content=ob_get_contents();//Assign the details page content to the $content variable  file_put_contents($goods _statis_file,$content);//write content to the corresponding static file  Ob_end_flush();//Export Product Details page information}?>

2. Use NoSQL to read content from memory (in fact, this is not static but caching);

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 the data from the database and assign the value to the relevant variable//include ("xxx.html");//load the corresponding Product detail page template  $content=ob_get_contents();//Assign the details page content to the $content variable  $mem->add ($goods _statis_content,$content,false,$expr); Ob_end_flush();//Export Product Details page information}?>

Memcached is the key value one by one corresponds, the key default maximum cannot exceed 128 bytes, the value default size is 1M, so 1M size satisfies most page size storage.
The above is the page static related methods, hoping to help friends
Update your technical articles every day at www.phpskill.com
PHP Pure Technology Learning Exchange Group: 323899029

Originally from: http://www.phpskill.com/html/show-1-4418-1.html

The above describes how to master a number of common page static methods, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.