Implement static pages, PHP is how to achieve?

With the increase in the content of the site and increase the amount of user access, inevitable is the site loads will become slower, limit the number of requests is limited by the bandwidth and server at the same time, we often need at this time for our website Code optimization and server configuration optimization.

We will do the optimization from the following general

1, dynamic static pages

2, optimizing the database

3, using load balancing

4, using the cache

5, using a CDN to accelerate

Pure static website in the site is how to achieve?
Purely static production technology is required to first page of the site summed up, is divided into a number of styles, then these pages made templates.

Generated when the need to read the source file and then generates a separate page files ending in .html, so that pure static Web sites need more space, but the space is actually needed will not be much bigger, especially for small and medium enterprises web sites, technically speaking, the station's large sites want to achieve pure static is more difficult, resulting in a long time too. But small and medium sites is made of relatively pure static, the advantages of doing so are many.

 

The dynamic website is how static processes?
Static pages refers to the dynamic page becomes html / htm static pages. Dynamic pages are generally written by asp, php, jsp, .net and other programming languages is made very easy to manage. But the program also needed to access the web first deal again, resulting in relatively slow access speed. The static page access speed, but not easy to manage. So dynamic static pages which can be focused on the benefits of two kinds of pages together.

 

Static site after treatment to bring the benefits?

1, static pages with respect to the dynamic pages more easily indexed by search engines.

2, static files do not need to go through processing procedures, it is possible to increase the operating speed.

3, to reduce the burden on the server.

4, HTML pages without affecting the Asp-related vulnerabilities affected.

The treated relatively static website is not static in terms of processing sites also have more security, because the site is not static is the preferred target for hackers because the hacker without knowing your back-end system, the hackers from the front desk Static page difficult to attack. It also has a certain stability, such as databases or Web site program out of the question, he would not interfere with page after static treatment, it will not affect the program or data can not open the page.

Search engine spiders prefer this URL, it can also reduce the burden spider, although some people think that search engines now have the ability to grab and fully recognize the dynamic URL, where it is recommended that we can make static try to make a static URL.

 

Here we mainly talk about the concept of static pages, I hope for your help!

What is HTML static:

Often said that the static pages are divided into two types, one is the pseudo-static , ie url rewriting, one is really static .
For website promotion and SEO and other needs, the need for the site in PHP web development or in the station's local static treatment, PHP generate static HTML pages there are a number of ways, such as the use of PHP templates, caching achieve static pages.
PHP static simple understanding is that the site generates static HTML pages to show the form in front of visitors, PHP static points pure static and pseudo-static, the difference is that PHP to generate static pages of different handling mechanism.
PHP pseudo-static : Use Apache mod_rewrite URL rewriting method of implementation.

 

HTML static benefits :

First, reduce the burden on the server, browse the web without calling the system database.
Second, there is conducive to search engine optimization SEO, Baidu, Google will give priority to collection of static pages, not only to be included in the fast also included full;
Third, speed up the page to open the speed, static pages without having to connect to the database open faster than dynamic pages has improved significantly;
four , more secure site, HTML pages will not be affected php program-related vulnerabilities; by looking at the bigger sites are all basically static pages, but also can reduce attacks, anti sql injection. When a database error does not affect normal access to the site.
Fifth, when a database error does not affect normal access to the site.
The most important is access speed can be increased, reducing the burden on the server, when the amount of data has tens of thousands, hundreds of thousands or more when you know which is faster. And also easily found by search engines. Although the operation generated html article on some trouble, procedurally complicated, but, in order to more conducive to search, in order to speed faster, safer, and these sacrifices worth it.

 

Achieve static HTML strategies and examples to explain:
the basic way
file_put_contents () function 
using php built-in cache mechanism to achieve static pages -output-bufferring.

Method 1: Using PHP templates to generate static pages

PHP template is very easy to implement static, such as installing and using PHP Smarty achieve static website.
In the case of Smarty, you can also implement static pages. The following briefly talk about the use of Smarty usual practice when a dynamic read. 
This is generally divided into steps:
1, passed through a URL argument (ID);
2, and based on this ID to query the database;
3, after obtaining the data necessary to modify the display contents;
. 4, the data required ASSIGN;
. 5, the display template file .
Smarty static process requires only two steps to add the above-described process.
First: 1 prior to use ob_start () Open buffer.
Second: Use after 5 ob_get_contents () Gets the memory is not output, and then use fwrite () writes the contents to the target html file.
According to the above description, this process is implemented in the web front, and content management (add, modify, delete) is usually in the background, in order to effectively use the above procedure, you can use a little tool that Header (). The specific process is as follows: add, modify, after the completion of the program, using the Header () reads the jump reception, this can be achieved of HTML pages, and then jump back to the side of the generated html admin then, these two processes Jump It is not visible.

Method 2: Use PHP file read and write functions to generate static pages

$out1 = "<html><head><title>PHP网站静态化教程</title></head><body>欢迎访问PHP网站开发www.startphp.cn,本文主要介绍PHP网站页面静态化的方法</body></html>";

$fp = fopen("leapsoulcn.html","w");

if(!$fp) {

  echo "System Error";

  exit();

} else {

  fwrite($fp,$out1);

  fclose($fp);

  echo "Success";

}

Method 3: Use PHP output control function (Output Control) / ob caching mechanism for generating static pages
output control function (Output Control) is used and controlled caching to generate static HTML pages, PHP will use the file read and write functions.
For example, a product of the dynamic details page address is: http://xxx.com?goods.php?gid=112
this time we read this story page based on this address, and then save it as a static page, the next time someone when accessing this product details page dynamic address, we can directly correspond to the output generated good out static content files.

PHP example code to generate static page 1 ob_start();  echo "<html>".

"<head>".

"<title>PHP网站静态化教程</title>".

"</head>".

"<body>欢迎访问脚本之家,本文主要介绍PHP网站页面静态化的方法</body>".

"</html>";

$out1 = ob_get_contents();

ob_end_clean();

$fp = fopen("leapsoulcn.html","w");

if(!$fp) {

   echo "System Error";

   exit();

} else {

   fwrite($fp,$out1);

   fclose($fp);

  echo "Success";

}

PHP example code to generate static pages 2$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();//输出商品详情页信息

}

We know that using PHP web development, general execution result output directly to the tour is, in order to generate static pages using PHP, you need to use the output control function to control the buffer zone in order to obtain the contents of the buffer, and then output to a static HTML page file in order to achieve static website.

PHP to generate static pages of ideas: first open the cache, and then output the HTML content (you can also include HTML content comes in the form of files via include), after obtaining the contents of the cache, the cache read and write functions through empty PHP file cache contents are written to a static HTML page file.
The process of obtaining the output cache content to generate static HTML pages requires the use of three functions: ob_start (), ob_get_contents () , ob_end_clean ().


Knowledge points:
1, generally ob_start function is mainly used to open the cache, be careful not to have any output, such as spaces, characters, etc. before using ob_start.
2, ob_get_contents function is mainly used to obtain the contents of the cache returned as a string, note that this function must be called before ob_end_clean function, not otherwise get the cached content.
3, ob_end_clean main function is to empty the contents of the cache and turn off caching, success, returns True, False if failed
Method 4: Use nosql read from memory (in fact, this is already not a static but rather a cache);
to memcache example:

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();//输出商品详情页信息

}

memcached is a key one to one, key default maximum can not exceed 128 bytes, value default size is 1M, 1M and therefore the size of the page size for most of storage.

Published 172 original articles · won praise 45 · views 40000 +

Guess you like

Origin blog.csdn.net/fish_study_csdn/article/details/103591192