PHP站点开发过程中,因为搜索引擎对PHP页面搜鹿和html页面的收录有一定的区别,为了站点的推广或者SEO的须要,要对站点进行一定的静态化。静态化并非页面中没有动画等元素,而是指网页的html代码都在页面中,不须要再去执行PHP脚本等server端的语言,我们能够直接訪问到的网页。这就是静态网页。

有一种方式是改写訪问地址,能够通过URL的PATHINFO模式来改动它。让它看上去更像一个静态页面。从而有更大的几率被搜索引擎抓取和收录,仅是对搜索引擎比較友好,伪静态化。

第二种就是站点能够在用户訪问站点之前就通过一定的程序来进行静态化。生成静态页面。当用户去訪问该页面的时候。因为訪问的是静态页面,因此,訪问速度会比訪问动态页面的速度快了非常多倍,前台的表现是页面载入速度变快,在后台的表现是降低了数据库的连接。降低了数据库的压力,唯一的缺点就是相对占的硬盘多一些,硬盘相对便宜的多。

纯静态化,就是生成HTML文件的方式,我们须要开启PHP自带的缓存机制,即ob_start来开启缓存。而且在ob_start之前不能有不论什么输出,否则运行失败,然后我们用ob_get_contents函数来获取缓存中的内容,该函数会返回一个字符串。第三个函数就是ob_end_clean,它用来清空缓存中的内容而且关闭,成功返回True,失败返回False。

<?php
//开启缓存
ob_start();
//第一步连接数据库
$conn = mysqli_connect("localhost","root","","bbs");
//第二步设置对应的字符编码
$setting = 'set names utf8';
mysqli_query($conn,$setting);
//第三步进行查询
$sql = 'SELECT * FROM user';
$result = mysqli_query($conn,$sql);
//第四步把查询结果转化为一个数组
$rows = mysqli_num_rows($result);
$sqldata = array();
for($i = 0;$i <$rows;$i ++){
$sqldata[] = mysqli_fetch_assoc($result);
}
//然后打印该信息
var_dump($sqldata);
//得到生成的html文件,下次訪问就无需訪问数据库了
$msg = ob_get_contents();
ob_end_clean();
//把输出内容放入一个html文件里
$f = fopen("static.html","w");
fwrite($f,$msg);
echo "静态化成功";

目录下生成一个html文件

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b> <i>(size=6)</i>
0 <font color='#888a85'>=></font>
<b>array</b> <i>(size=4)</i>
'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i>
'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'0'</font> <i>(length=1)</i>
'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'辛星'</font> <i>(length=6)</i>
'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'bd04fcc97578ce33ca5fb331f42bc375'</font> <i>(length=32)</i>
1 <font color='#888a85'>=></font>
<b>array</b> <i>(size=4)</i>
'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'2'</font> <i>(length=1)</i>
'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i>
'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'小倩'</font> <i>(length=6)</i>
'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'61cb72858be523b9926ecc3d7da5d0c6'</font> <i>(length=32)</i>
2 <font color='#888a85'>=></font>
<b>array</b> <i>(size=4)</i>
'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'3'</font> <i>(length=1)</i>
'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i>
'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'小楠'</font> <i>(length=6)</i>
'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'a3d2de7675556553a5f08e4c88d2c228'</font> <i>(length=32)</i>
3 <font color='#888a85'>=></font>
<b>array</b> <i>(size=4)</i>
'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'4'</font> <i>(length=1)</i>
'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i>
'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'刘强'</font> <i>(length=6)</i>
'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'fcdb06a72af0516502e5fdccc9181ee0'</font> <i>(length=32)</i>
4 <font color='#888a85'>=></font>
<b>array</b> <i>(size=4)</i>
'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'5'</font> <i>(length=1)</i>
'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i>
'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'星哥'</font> <i>(length=6)</i>
'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'866a6cafcf74ab3c2612a85626f1c706'</font> <i>(length=32)</i>
5 <font color='#888a85'>=></font>
<b>array</b> <i>(size=4)</i>
'id' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'6'</font> <i>(length=1)</i>
'level' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'1'</font> <i>(length=1)</i>
'name' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'辛勇'</font> <i>(length=6)</i>
'pwd' <font color='#888a85'>=></font> <small>string</small> <font color='#cc0000'>'e93beb7663f3320eaa0157730d02dd0c'</font> <i>(length=32)</i>
</pre>

这样浏览器直接訪问html文件,从而减轻了数据库的压力。

最新文章

  1. 开放api接口签名验证
  2. tcpdump高级过滤技巧
  3. Photoshop的评价
  4. ECLIPSE ANDROID PROJECT IMPORT SUMMARY
  5. NSString的八条实用技巧
  6. 关于DCMTK3.6.0源代码编译的总结
  7. UESTC 1215 (思维题 旋转)
  8. 我的Android 4 学习系列之Intent 和 Broadcast Reciever
  9. Android 动态加载(防止逆向编译) jar混淆加密
  10. Python爬虫【解析库之pyquery】
  11. 用python给邮箱发邮件,问题,以及解决方法。
  12. JAVA中值类型和引用类型的不同(面试常考)
  13. 通过wifi 连接 adb 到 手机
  14. 若sql语句中order by指定了多个字段,怎么排序?
  15. 20、uwp打包失败(All app package manifests in a bundle must declare the same values under the XPath *[local-name()=&#39;Package&#39;]/*[local-name()=&#39;Dependencies&#39;])
  16. Postman—前置请求脚本
  17. Struts-config.xml配置文件《action-mappings》元素的详解
  18. LintCode 407: Plus One
  19. 窗体彻底关闭事件FormClosed
  20. .CHM文件使用问题

热门文章

  1. Git review :error: unpack failed: error Missing tree
  2. usb_submit_urb
  3. git中Untracked files如何清除
  4. CodeForces 450B Jzzhu and Sequences(矩阵快速幂)题解
  5. 【建项目】eclipse maven建立多模块工程
  6. P4factory ReadMe Quickstart 安装p4factory
  7. POJ 3613 Cow Relays(floyd+快速幂)
  8. Android 旋转、平移、缩放和透明度渐变的补间动画
  9. xtu 1242 Yada Number 容斥原理
  10. shell 按行读取文件