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 = '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文件,從而減輕了數據庫的壓力。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM