PHP頁面靜態化

buffer    存儲數據php

buffer 其實就是緩衝區,一個內存地址空間,主要用於存儲數據區域。html

 

輸出流程mysql

內容-->php buffer --> tcp --> 終端jquery

 

在PHP  ini 文件中有一個output_buffering = onnginx

獲取緩衝區當中的數據   ob_get_contents();ajax

    echo 1;
    echo '<br/>';
    echo ob_get_contents();

若是在php.ini文件中output_buffering沒有開啓可使用 ob_start()正則表達式

ob_start()  開啓緩衝區sql

 

PHP如何實現頁面純靜態化數據庫

基本方式:apache

一、file_put_contents()函數:將一個字符串寫入文件  返回寫入到文件按內的字節數,失敗返回FALSE

二、PHP內置緩存機制實現頁面靜態化  - output_buffering

 

 

如何觸發系統生成純靜態化頁面

一、頁面添加緩存時間

二、手動觸發方式

三、crontab定時掃描程序

 

 

頁面添加緩存時間

用戶請求頁面---->頁面時間是否過時----->是  【動態頁面並生成一份新的靜態頁面】

                  否   【獲取靜態頁面】

is_file('./index.html') && (time()-filemtime('./index.html') < 1200)

若是咱們的服務器存在這個文件,而且當前時間減去文件最後修改時間 小於 1200秒  認爲緩存沒有失效,直接加載靜態文件。不然從新生成。

filemtime()獲取文件修改時間

 

手動觸發方式

在後臺開闢一個功能,針對首頁作一個  開始更新,點擊開始更新,走的程序是沒有if判斷最初的純靜態頁面實現程序

 

crontab定時掃描程序

在Linux下,

crontab -e 編輯crontab

*/5****php/data/static/index.php

第一個*表明分

第二個*表明時

第三個*表明日

第四個*表明月

第五個*表明周

*/5****    */5表明每五分鐘

*/5****php/data/static/index.php

告訴Linux服務器每5分鐘執行一次這個程序,生成純靜態頁面

 

 

**
 * 處理頁面靜態化業務邏輯
 * 有3種方案, 第一:定時(利用crontab來處理)  第二:人爲觸發 第三:在頁面中控制時間來操做
*/
//header("content-type:text/htm;charset=utf-8");
if(is_file('./index.html') && (time()-filemtime('./index.html') < 1200)) {
    require_once('./index.html');
}else {
    // 引入數據庫連接操做
    require_once('./db.php');
    $sql = "select * from news where `category_id` = 1 and `status` = 1 limit 4";
    try{
        $db = Db::getInstance()->connect();
        $result = mysql_query($sql, $db);
        $newsList = array();
        while($row = mysql_fetch_assoc($result)) {
            $newsList[] = $row;
        }
    }catch(Exception $e) {
        // TODO
    }
    ob_start();
    require_once('template/index.php');
    $s = ob_get_contents();
    file_put_contents('./index.html', $s);
    //ob_clean();
}

 

局部動態化

實現步驟:編寫接口-->ajax請求接口操做

靜態化頁面中若是想加載動態的內容如何處理?

ajax技術

jquery中ajax請求方式

$.ajax({
    url: '',  //請求服務器端的接口地址
    type: 'get',
    dataType: 'json',
    error: function(){
    },
    success: function(result){
    }
})

 

 

僞靜態

分析:經過正則表達式去分析僞靜態URL地址

例:http://static.com/newsList.php?type=2&category_id=1 =>  http://static.com/newsList.php/2/1.html

其中: 2-->type=2,  1-->category_id=1

$_SERVER:服務器當中的server變量  print_r($_SERVER)

其中[PATH_INFO]的值 => /2/1.html

 

preg_match() 函數用於進行正則表達式匹配,成功返回 1 ,不然返回 0 。

語法:

int preg_match( string pattern, string subject [, array matches ] )
pattern 正則表達式
subject 須要匹配檢索的對象
matches 可選,存儲匹配結果的數組, $matches[0] 將包含與整個模式匹配的文本,$matches[1] 將包含與第一個捕獲的括號中的子模式所匹配的文本,以此類推
preg_match("/^\/(\d+)\/(\d+)(\.html)$/", $_SERVER['PATH_INFO'], $pathInfo)

\d 匹配0-9當中的一個數字    \d+  匹配一個或者多個     $pathInfo賦給這個變量

接着進行匹配

if(preg_match("/^\/(\d+)\/(\d+)(\.html)$/", $_SERVER['PATH_INFO'], $pathInfo)) {
  //var_dump($pathInfo);
  $type = $pathInfo[1]; // 類型值
  $categoryId = $pathInfo[2]; // 所在欄目值
  // 引入數據庫連接類
  require_once('./db.php'); 
  $sql = "select * from news where `category_id` = ".$categoryId." and `type` = ".$type." order by id desc";
  //  提取數據以後組裝好放入模板中
  //...
} else { // TODO }

其中$_SERVER['PATH_INFO']

 

/**
* 利用PHP正則表達式來處理僞靜態
* 以http://static.com/newsList.php?type=2&category_id=1 =>  http://static.com/newsList.php/2/1.shtml
*/
//echo 12;
var_dump($_SERVER);

if(isset($_SERVER['PATH_INFO'])) {
    // 解析 /2/1.shtml 匹配pathinfo值,若是沒匹配到則數據不合法,若匹配到作相應處理
    if(preg_match("/^\/(\d+)\/(\d+)(\.shtml)$/", $_SERVER['PATH_INFO'], $pathInfo)) {
        //var_dump($pathInfo);
        $type = $pathInfo[1]; // 類型值
        $categoryId = $pathInfo[2]; // 所在欄目值
        // 引入數據庫連接類
        require_once('./db.php'); 
        $sql = "select * from news where `category_id` = ".$categoryId." and `type` = ".$type." order by id desc";
        try{
            $db = Db::getInstance()->connect();
            $result = mysql_query($sql, $db);
            $newsList = array();
            while($row = mysql_fetch_assoc($result)) {
                $newsList[] = $row;
            }
            var_dump($newsList);
            exit;
        }catch(Exception $e) {
            // TODO
        }
    } else {
        // TODO
        die('url error!');
    }
} else {
    // TODO
    // 錯誤提示,而後跳轉到首頁處理
    die('地址有誤');
}

 

WEB服務器rewrite配置  達到僞靜態的目的

apache下rewrite配置

一、虛擬域名配置

二、httpd_vhosts.conf配置文件配置相關信息

虛擬域名配置:

C:\work\wamp64\bin\apache\apache2.4.17\conf   下  httpd.conf

一、httpd.conf文件中開啓相關模式

      LoadModule rewrite_module modules/mod_rewrite.so

      Include conf/extra/httpd-vhosts.conf

      vhosts文件增長相關域名   (ServerName)

C:\work\wamp64\bin\apache\apache2.4.17\conf\extra   --->    httpd-vhosts.conf   --->ServerName

二、httpd_vhosts.conf配置文件配置相關信息(虛擬域名的配置文件)

DocumentRoot:  項目目錄

ServerName:虛擬域名

 繼續配置hosts文件:C:\Windows\System32\drivers\etc\  hosts

 

僞靜態配置案例:

httpd-vhosts.conf  下配置

  RewriteEngine on    //開啓

  RewriteRule^/detail/([0-9]*).html$ /detail.php?id=$1     //規則

  $1 是 ([0-9]*)

  例:state.com/detail/134.html    ==    state.com/detail.php?id=134

 #RewriteCond  兩句話的意思  執行純靜態化內容

  -d 表明目錄

  -f 表明文件

  意思:當咱們服務器存在這個目錄或者存在這個文件,這時候就讓他去訪問這個目錄或者文件。。沒有就會訪問動態內容。

 

 

nginx下rewrite配置

   rewrite ^/detail/(\d+)\.html$ /detail.php?id=$1 last;

   rewrite     匹配規則       對應到的動態文件

虛擬機或者服務器下   /etc/nginx/conf.d文件下

static.singwa.com.conf文件

相關文章
相關標籤/搜索