php頁面靜態化,php頁面靜態_PHP教程

來源:互聯網
上載者:User

php頁面靜態化,php頁面靜態


如何最佳化頁面回應時間:

  • 動態網頁面靜態化
  • 最佳化資料庫
  • 使用負載平衡
  • 使用緩衝

如果頁面中的一些內容不經常改動,可以使用動態網頁面靜態化。好處是:減少伺服器指令碼的計算時間;降低伺服器的回應時間。

1、動態URL地址設定靜態形式(偽靜態)

例如:http://xxx.com/index.php?c=play&id=1111 =>

http://xxx.com/play/1111.html (並不是一個純靜態頁面)

2、buffer

buffer其實就是緩衝區,一個記憶體位址空間,主要用於儲存資料區域。

編寫一個buffer.php檔案,並儲存,並不是直接將檔案內容儲存在磁碟裡,而是先把內容寫入到buffer中,當一個buffer寫滿的時候,會把buffer中的資料寫入到磁碟裡,這是作業系統的buffer。

當執行一個PHP程式的時候,如果有輸出內容,會先放到輸出緩衝區,資料再通過tcp傳給用戶端或瀏覽器。

要想資料能夠放到輸出緩衝區,首先開啟輸出緩衝,通過php.ini檔案output_buffering = On或者ob_start(),然後使用ob_get_contents()擷取輸出緩衝區內容。

3、PHP實現頁面純靜態化

純靜態化的html檔案放在伺服器端的磁碟。

基本方式:

  • file_put_contents()函數;

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )

   成功會返回寫入到檔案內資料的位元組數,失敗時返回false。

  • 使用PHP內建緩衝機制實現頁面靜態化-output_buffering

  ob函數

   ob_start(); 開啟輸出緩衝區

   ob_get_contents(void);返回輸出緩衝區內容

   ob_clean(void);清空輸出緩衝區

   ob_get_clean(void);得到當前緩衝區的內容並刪除當前輸出緩衝區

產生純靜態頁面的三種方式

  • 頁面添加緩衝時間
php
//存在index.html並且在有效時間以內(5分鐘)if (file_exists('index.html') && (time()-filemtime('index.html') < 300)) { require_once 'index.html';}else{ ob_start(); //串連資料庫擷取資料並填充到模板 echo 'helllo world'; file_put_contents('index.html', ob_get_contents()); }?>
  • 手動觸發

  後台手動設定,主動產生

  • linux crontab 定時掃描程式

  crontab -e //編輯某個crontab檔案,檔案內容如:*/5 * * * * php執行程式所在目錄 /xx/xx.php

  http://www.cnblogs.com/peida/archive/2013/01/08/2850483.html

4、偽靜態

PHP處理偽靜態:Regex匹配

//http://xxx.cn/xx/test4.php/2/11.html//http://xxx.cn/xx/test4.php?page=2&id=1,實際的訪問路徑if(preg_match('/\/(\d+)\/(\d+).html/', $_SERVER['PATH_INFO'], $matches)){     $param['page'] = $matches[1];     $param['id'] = $matches[2];}

Apache下rewrite配置

http://myapps.com/detail/12.html ==> http://myapps.com/apps/detail.php?id=12(實際訪問的路徑)

httpd.conf檔案:開啟LoadModule rewrite_module modules/mod_rewrite.so

extra/httpd-vhosts.conf檔案 作如下配置:


ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "D:/wamp/www/myProject"
ServerName myapps.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common

RewriteEngine on

#如果detail目錄下有12.html檔案,就優先訪問該目錄下的檔案
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteRule ^/detail/([0-9]*).html$ /apps/detail.php?id=$1

http://www.onexin.net/apache-rewrite-detailed/

http://www.bkjia.com/PHPjc/1096610.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1096610.htmlTechArticlephp頁面靜態化,php頁面靜態 如何最佳化頁面回應時間: 動態網頁面靜態化 最佳化資料庫 使用負載平衡 使用緩衝 如果頁面中的一些內容不經常改...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.