Home > Web Front-end > HTML Tutorial > Master several common methods of page staticization_html/css_WEB-ITnose

Master several common methods of page staticization_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:50:45
Original
1296 people have browsed it

It is often said that page staticization is divided into two types, one is pseudo-static, that is, url rewriting, and the other is true staticization. Let’s focus on true staticization.

What is PHP static

The simple understanding of PHP static is to make the website-generated pages displayed in front of visitors in the form of static HTML. PHP static is divided into pure static and pseudo-static. The difference between the two lies in the different processing mechanisms used by PHP to generate static pages.


Why make web pages static?
1. Speed ​​up page opening and browsing speed. Static pages do not need to connect to the database and are significantly faster than dynamic pages.
2. Conducive to search engine optimization SEO, Baidu, and Google will give priority to including static pages, which are not only included quickly but also included completely;
3. Reduce the burden on the server, browsing the web without calling the system database;
4. The website is more secure, and HTML pages will not Affected by PHP-related vulnerabilities; Look at larger websites, which are basically static pages, and can reduce attacks and prevent SQL injection.
When a database error occurs, normal access to the website will not be affected.
Although the operation of generating html articles is more troublesome and the procedures are more complicated, in order to be more convenient for search, faster and safer, these sacrifices are still worth it.


How to generate static HTML pages with PHP

Use PHP templates to generate static pages

PHP templates are very convenient to achieve staticization, such as installing and using PHP Smarty to implement websites To make it static, you can also write your own set of template parsing rules. Common template rules can imitate various CMS templates.


1. Use PHP file reading and writing functions and ob caching mechanism to generate static pages
For example, the address of the dynamic details page of a product is: http://xxx.com?goods.php? gid=112
Then here we read the content of this details page based on this address, and then save it as a static page. Next time someone visits the dynamic address of this product details page, we can
directly save the generated The corresponding static content file is output.

<!--?php$gid  = $_GET['gid']+0;//商品id$goods_statis_file = "goods_file_".$gid.".html";//对应静态页文件$expr = 3600*24*10;//静态文件有效期,十天if(file_exists($goods_statis_file)){      $file_ctime =filectime($goods_statis_file);//文件创建时间       if($file_ctime+$expr-->time()){//如果没过期         echo file_get_contents($goods_statis_file);//输出静态文件内容         exit;      }else{//如果已过期            unlink($goods_statis_file);//删除过期的静态页文件            ob_start();            //从数据库读取数据,并赋值给相关变量            //include ("xxx.html");//加载对应的商品详情页模板            $content = ob_get_contents();//把详情页内容赋值给$content变量            file_put_contents($goods_statis_file,$content);//写入内容到对应静态文件中            ob_end_flush();//输出商品详情页信息      }}else{  ob_start();  //从数据库读取数据,并赋值给相关变量  //include ("xxx.html");//加载对应的商品详情页模板  $content = ob_get_contents();//把详情页内容赋值给$content变量  file_put_contents($goods_statis_file,$content);//写入内容到对应静态文件中  ob_end_flush();//输出商品详情页信息}?>
Copy after login

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

Take memcache as an example:

<!--?php$gid  = $_GET['gid']+0;//商品id$goods_statis_content = "goods_content_".$gid;//对应键$expr = 3600*24*10;//有效期,十天$mem = new Memcache; $mem--->connect('memcache_host', 11211);$mem_goods_content = $mem->get($goods_statis_content);if($mem_goods_content){      echo $mem_goods_content;}else{  ob_start();  //从数据库读取数据,并赋值给相关变量  //include ("xxx.html");//加载对应的商品详情页模板  $content = ob_get_contents();//把详情页内容赋值给$content变量  $mem->add($goods_statis_content,$content, false, $expr);  ob_end_flush();//输出商品详情页信息}?>
Copy after login

Memcached has a one-to-one correspondence between keys and values. The default maximum key size cannot exceed 128 bytes, and the default size of value is 1M, so the 1M size can meet the storage needs of most web pages.
The above are the related methods of page staticization, I hope it will be helpful to friends
Excellent technical articles are updated every day, all at www.phpskill.com
Pure pure technology of php Learning exchange group: 323899029

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template