hellohell

     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文件,从而减轻了数据库的压力。

 

分类:

技术点:

相关文章:

  • 2021-06-08
  • 2022-02-18
  • 2022-01-25
  • 2021-12-28
  • 2021-06-05
  • 2022-02-07
  • 2022-01-20
猜你喜欢
  • 2022-12-23
  • 2021-11-29
  • 2022-02-08
  • 2022-01-07
  • 2022-01-05
  • 2022-12-23
相关资源
相似解决方案