Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php实现静态化方法是什么

php实现静态化方法是什么

coldplay.xixi
coldplay.xixiasal
2020-10-30 16:07:342601semak imbas

php实现静态化的方法:1、改写访问地址,能够通过URL的PATHINFO模式来改动它;2、站点能够在用户访问站点之前就通过一定的程序来进行静态化。

php实现静态化方法是什么

php实现静态化的方法:

 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 = &#39;set names utf8&#39;;
mysqli_query($conn,$setting);
//第三步进行查询
$sql = &#39;SELECT * FROM user&#39;;
$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=&#39;xdebug-var-dump&#39; dir=&#39;ltr&#39;>
<b>array</b> <i>(size=6)</i>
  0 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;0&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;辛星&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;bd04fcc97578ce33ca5fb331f42bc375&#39;</font> <i>(length=32)</i>
  1 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;2&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;小倩&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;61cb72858be523b9926ecc3d7da5d0c6&#39;</font> <i>(length=32)</i>
  2 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;3&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;小楠&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;a3d2de7675556553a5f08e4c88d2c228&#39;</font> <i>(length=32)</i>
  3 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;4&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;刘强&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;fcdb06a72af0516502e5fdccc9181ee0&#39;</font> <i>(length=32)</i>
  4 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;5&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;星哥&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;866a6cafcf74ab3c2612a85626f1c706&#39;</font> <i>(length=32)</i>
  5 <font color=&#39;#888a85&#39;>=></font> 
    <b>array</b> <i>(size=4)</i>
      &#39;id&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;6&#39;</font> <i>(length=1)</i>
      &#39;level&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;1&#39;</font> <i>(length=1)</i>
      &#39;name&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;辛勇&#39;</font> <i>(length=6)</i>
      &#39;pwd&#39; <font color=&#39;#888a85&#39;>=></font> <small>string</small> <font color=&#39;#cc0000&#39;>&#39;e93beb7663f3320eaa0157730d02dd0c&#39;</font> <i>(length=32)</i>

相关学习推荐:php编程(视频)

Atas ialah kandungan terperinci php实现静态化方法是什么. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn