溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php如何實(shí)現(xiàn)靜態(tài)化方法

發(fā)布時間:2020-11-02 10:51:12 來源:億速云 閱讀:159 作者:小新 欄目:編程語言

php如何實(shí)現(xiàn)靜態(tài)化方法?這個問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個問題能讓你收獲頗深。下面是小編給大家?guī)淼膮⒖純?nèi)容,讓我們一起來看看吧!

php實(shí)現(xiàn)靜態(tài)化的方法:1、改寫訪問地址,能夠通過URL的PATHINFO模式來改動它;2、站點(diǎn)能夠在用戶訪問站點(diǎn)之前就通過一定的程序來進(jìn)行靜態(tài)化。

php實(shí)現(xiàn)靜態(tài)化的方法:

PHP站點(diǎn)開發(fā)過程中,因?yàn)樗阉饕鎸HP頁面搜鹿和html頁面的收錄有一定的區(qū)別,為了站點(diǎn)的推廣或者SEO的須要,要對站點(diǎn)進(jìn)行一定的靜態(tài)化。靜態(tài)化并非頁面中沒有動畫等元素,而是指網(wǎng)頁的html代碼都在頁面中,不須要再去執(zhí)行PHP腳本等server端的語言,我們能夠直接訪問到的網(wǎng)頁。這就是靜態(tài)網(wǎng)頁。

有一種方式是改寫訪問地址,能夠通過URL的PATHINFO模式來改動它。讓它看上去更像一個靜態(tài)頁面。從而有更大的幾率被搜索引擎抓取和收錄,僅是對搜索引擎比較友好,偽靜態(tài)化。

第二種就是站點(diǎn)能夠在用戶訪問站點(diǎn)之前就通過一定的程序來進(jìn)行靜態(tài)化。生成靜態(tài)頁面。當(dāng)用戶去訪問該頁面的時候。因?yàn)樵L問的是靜態(tài)頁面,因此,訪問速度會比訪問動態(tài)頁面的速度快了非常多倍,前臺的表現(xiàn)是頁面載入速度變快,在后臺的表現(xiàn)是降低了數(shù)據(jù)庫的連接。降低了數(shù)據(jù)庫的壓力,唯一的缺點(diǎn)就是相對占的硬盤多一些,硬盤相對便宜的多。

純靜態(tài)化,就是生成HTML文件的方式,我們須要開啟PHP自帶的緩存機(jī)制,即ob_start來開啟緩存。而且在ob_start之前不能有不論什么輸出,否則運(yùn)行失敗,然后我們用ob_get_contents函數(shù)來獲取緩存中的內(nèi)容,該函數(shù)會返回一個字符串。第三個函數(shù)就是ob_end_clean,它用來清空緩存中的內(nèi)容而且關(guān)閉,成功返回True,失敗返回False。

<?php
//開啟緩存
ob_start();
//第一步連接數(shù)據(jù)庫
$conn = mysqli_connect("localhost","root","","bbs");
//第二步設(shè)置對應(yīng)的字符編碼
$setting = 'set names utf8';
mysqli_query($conn,$setting);
//第三步進(jìn)行查詢
$sql = 'SELECT * FROM user';
$result = mysqli_query($conn,$sql);
//第四步把查詢結(jié)果轉(zhuǎn)化為一個數(shù)組
$rows = mysqli_num_rows($result);
$sqldata = array();
for($i = 0;$i <$rows;$i ++){
    $sqldata[] = mysqli_fetch_assoc($result);
}
//然后打印該信息
var_dump($sqldata);
//得到生成的html文件,下次訪問就無需訪問數(shù)據(jù)庫了
$msg = ob_get_contents();
ob_end_clean();
//把輸出內(nèi)容放入一個html文件里
$f = fopen("static.html","w");
fwrite($f,$msg);
echo "靜態(tài)化成功";

目錄下生成一個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'>'劉強(qiáng)'</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>

感謝各位的閱讀!看完上述內(nèi)容,你們對php如何實(shí)現(xiàn)靜態(tài)化方法大概了解了嗎?希望文章內(nèi)容對大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI