Home >php教程 >php手册 >Share several common methods of page staticization_php basics

Share several common methods of page staticization_php basics

WBOY
WBOYOriginal
2016-05-16 08:59:483505browse

what is php static

a simple understanding of php staticization is to make the website-generated pages appear in front of visitors in the form of static html. php staticization is divided into pure staticization and pseudo-staticization. the difference between the two lies in the different processing mechanisms for 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. it is 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 and browse the web without calling the system database;
4. the website is more secure, and html pages will not be affected by php-related vulnerabilities; if you look at larger websites, they are basically static pages, and they can reduce attacks and prevent sql injection.

when a database error occurs, normal access to the website will not be affected.
although generating html articles is more troublesome in operation and more complicated in procedures, these sacrifices are still worth it in order to be more convenient for search, faster and safer.

how to generate static html pages with php

use php templates to generate static pages

it is very convenient to realize staticization of php templates. for example, you can install and use php smarty to realize static website. 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 a product's dynamic details page is: http://xxx.com?goods.php?gid=112
so 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 output the corresponding static content file that has been generated.

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

2. use nosql to read content from memory (in fact, this is no longer static but cached);

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

memcached has a one-to-one correspondence between keys and values. the default key size cannot exceed 128 bytes, and the default value size is 1m. therefore, the 1m size can meet the storage needs of most web pages.
the above are the relevant methods of page staticization. i hope it will be helpful to my friends

Statement:
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