Home > Backend Development > PHP Tutorial > 掌握常见的几种页面静态化的步骤

掌握常见的几种页面静态化的步骤

WBOY
Release: 2016-06-13 12:13:33
Original
998 people have browsed it

掌握常见的几种页面静态化的方法

常说的页面静态化分为两种,一种是伪静态,即url 重写,一种是真静态化。我们以真静态化为主来讲讲。

什么是PHP静态化

PHP静态化的简单理解就是使网站生成页面以静态HTML的形式展现在访客面前,PHP静态化分纯静态化和伪静态化,两者的区别在于PHP生成静态页面的处理机制不同。


为什么要让网页静态化
 一、加快页面打开浏览速度,静态页面无需连接数据库打开速度较动态页面有明显提高;
 二、有利于搜索引擎优化SEO,Baidu、Google都会优先收录静态页面,不仅被收录的快还收录的全;
 三、减轻服务器负担,浏览网页无需调用系统数据库;
 四、网站更安全,HTML页面不会受php相关漏洞的影响; 观看一下大一点的网站基本全是静态页面,而且可以减少攻击,防sql注入。
 数据库出错时,不影响网站正常访问。
 生成html文章虽操作上麻烦些,程序上繁杂些,但为了更利于搜索,为了速度更快些,更安全,这些牺牲还是值得的。


PHP生成静态HTML页面的方法

利用PHP模板生成静态页面

PHP模板实现静态化非常方便,比如安装和使用PHP Smarty实现网站静态化,也可以自己写一套模板解析规则,常见的可以模仿各类cms的模板规则。


1.使用PHP文件读写功能与ob缓存机制生成静态页面
比如某个商品的动态详情页地址是: http://xxx.com?goods.php?gid=112
那么这里我们根据这个地址读取一次这个详情页的内容,然后保存为静态页,下次有人访问这个商品详情页动态地址时,我们可以
直接把已生成好的对应静态内容文件输出出来。

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

2.使用nosql从内存中读取内容(其实这个已经不算静态化了而是缓存);

以memcache为例:

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

memcached是键值一一对应,key默认最大不能超过128个字节,value默认大小是1M,因此1M大小满足大多数网页大小的存储。
以上就是页面静态化的相关方法,希望对朋友们有所帮助
每天更新精彩技术文章,尽在www.phpskill.com
php纯技术学习交流群: 323899029

原文来自:http://www.phpskill.com/html/show-1-4418-1.html

Related labels:
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