To share several common ways of page static

Source: Internet
Author: User
Tags php template

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.

1<!--?PHP2 $gid=$_get[' GID ']+0;//Product ID3 $goods _statis_file= "Goods_file_".$gid.". HTML ";//corresponding static page file4 $expr= 3600*24*10;//static file validity, 10 days5 if(file_exists($goods _statis_file)){6   $file _ctime=Filectime($goods _statis_file);//File creation Time7      if($file _ctime+$expr- Time()){//If not expired8       Echo file_get_contents($goods _statis_file);//output static file contents9          Exit;Ten}Else{//if it has expired One          unlink($goods _statis_file);//delete an outdated static page file A          Ob_start(); -    -             //read data from a database and assign values to related variables the    - //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 content to the corresponding static file -             Ob_end_flush();//Export Product Details page information +      } A}Else{ at  Ob_start(); -    -  //read data from a database and assign values to related variables -    - //include ("xxx.html");//load the corresponding Product Details page template -    in  $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 to  Ob_end_flush();//Export Product Details page information +    - } the    *?>

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

Take Memcache as an example:

1<!--?PHP2 $gid=$_get[' GID ']+0;//Product ID3 $goods _statis_content= "Goods_content_".$gid;//corresponding Key4 $expr= 3600*24*10;//valid for 10 days5   6 $mem=NewMemcache;7 $mem--->connect (' memcache_host ', 11211);8   9 $mem _goods_content=$mem->get ($goods _statis_content);Ten    One    A    - if($mem _goods_content){ -   Echo $mem _goods_content; the}Else{ -  Ob_start(); -    -  //read data from a database and assign values to related variables +    - //include ("xxx.html");//load the corresponding Product Details page template +    A  $content=ob_get_contents();//Assign the details page content to the $content variable at  $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

To share several common ways of page static

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.