PHP文件執行階段:語法分析-》編譯-》運行php
靜態html文件執行順序:運行html
動態程序:mysql
鏈接數據庫服務器或者緩存服務器->獲取數據->填充到模板->呈現給用戶jquery
關於優化頁面響應時間linux
1. 動態頁面靜態化:頁面中一些內容不常常改動nginx
不適用於內容常常變換的應用:例如:微博等。若是頁面中一些內容改動,動態頁面靜態化是很是有效的加速方法。ajax
動態頁面靜態化好處:正則表達式
2. 動態URL地址設置靜態形式(僞靜態)sql
3. php靜態化: 1.純靜態:數據庫
a. 局部純靜態
b. 所有純靜態
2. 僞靜態
4.Buffer認知(php.ini文件中output_buffering=on開啓緩存區,ob_start()也能夠開啓緩存區)
a) Buffer其實就是緩衝區,一個內存地址空間,主要用於數據存儲區域。
b)輸出流程:內容-》php buffer->tcp->終端
c)獲取緩衝區內容:php內容函數 ob_get_contents();
d)ob_start():開啓緩衝區(php5.3默認開啓output_buffering=on)
5.php如何實現頁面純靜態化
a)基本方式
i.file_put_contents()函數:將一個字符串寫入文件
file_put_contents(「文件名」,」文件內容」);
ii.使用PHP內置緩存機制實現頁面靜態化-output_buffering
OB函數
a) ob_start :打開輸出控制緩衝
b) ob_get_contents:返回輸出緩衝區內容
c) ob_clean:清空(擦掉)輸出緩衝區
d) ob_get_clean:獲得當前緩衝區的內容並刪除當前輸出緩衝區
<?php ob_start(); echo 1234; file_put_contents('index.shtml',ob_get_contents()); ?>
在瀏覽器(終端)輸出1234;index.shtml內容是:1234
<?php ob_start(); echo 1234; file_put_contents('index.shtml',ob_get_contents); ob_clean(); ?>
瀏覽器終端不會再有1234輸出,靜態頁面index.shtml輸出依舊是1234。理解php輸出流程:內容->php buffer->tcp->終端, ob_clean();
<?php ob_start(); echo 456789; file_put_contents('index.shtml',ob_get_clean()); ?>
瀏覽器終端無輸出,靜態文件index.shtml內容是:456789,ob_get_clean()至關於ob_get_contents()和ob_clean()兩個的做用。
b)純靜態化案例之步驟解析
i. 連接數據庫,而後從數據庫裏面獲取數據
ii. 把獲取的數據填充到模板文件裏面
iii. 須要把動態的頁面轉化爲靜態頁面,生成純靜態化文件
<?php require_once('./db.php'); $connect=Db::getInstance()->connect(); $sql="select * from news limit 5": $result=mysql_query($sql,$connect); $news=array(); while($row=mysql_fetch_array($result)){ $news[]=$row; } //開啓緩存區 ob_start(); //引入模板文件 require_once(./templates/abc.php); if(file_put_contents('index.shtml',ob_get_clean())){ echo 'success'; }else{ echo 'error'; } ?>
模板文件使用$news:abc.php
<ul> <?php foreach($news as $k=>$v)?> <li><a href="/" target="_blank"><?php echo $v["title"];?></a></li> <?php } ?> </ul>
6.如何觸發系統生成純靜態化頁面
頁面添加緩存時間:
<?php //filemtime():得到文件修改時間 if(is_file(./index.shtml)&&(time()-filemtime('./index.shtml'))<300){ require_once('./index.shtml'); }else{ require_once('./db.php'); $connect=Db::getInstance()->connect(); $sql="select * from news limit 5": $result=mysql_query($sql,$connect); $news=array(); while($row=mysql_fetch_array($result)){ $news[]=$row; } //開啓緩存區 ob_start(); //引入模板文件 require_once(./templates/abc.php); file_put_contents('index.shtml',ob_get_contents()); } ?>
手動觸發方式:後臺開發一個功能:更新模塊生成html靜態頁面(按鈕)
crontab定時掃描程序(linux系統):Linux服務器一個工具
i. 設置crontab只須要執行一個命令*/5****php/data/static/index.php(每5分鐘去執行一個程序)
局部動態化案例實現
a)靜態化頁面中如何想加載動態的內容如何處理?
Ajax技術:jquery中ajax請求方式$.ajax()
實現步驟:編寫接口-》ajax請求接口操做
接口數據:1.獲取數據
2. 把咱們獲取到的數據組裝成接口數據通訊
<?php //接口數據 //1.獲取數據 //2.把咱們獲取到的數據組裝成接口數據通訊 require_once("../db.php"); $connect=Db::getInstance()->connect(); $sql="select * from news limit 5"; $result=mysql_query($sql,$connect); while($row=mysql_fetch_assoc($result)){ $res[]=$row; } return show(1,'success',$res); function show($code=0,$message="error",$data=array()){ $result=array( 'code'=>$code, 'message'=>$message, 'data'=>$data, ); echo json_encode($result); } ?>
模板頁abc.php引入ajax文件代碼:
code=1是數據返回成功
$.ajax({ url:"http://abc.com/api/hot.php", type:'get', dataType:'json', error:function(){ }, success:function(result){ if(result.code==1){ html=''; $each(result.data,function(key,value){ html+='<li><a href="/">'+value.title+'</a></li>'; }) ; $("#hot_html").html(html); }else{ //todo } } });
僞靜態
a)php處理僞靜態(php的正則表達式方式)
i.(Apache服務器)案例分析(path_info模式)print_r($_SERVER)查看path_info /newsList.php/2/1.html
備註:nginx服務器默認狀況下不支持path_info模式,須要配置
<?php /* 經過正則表達式去分析僞靜態URL地址 http://abc.com/newList.php?type=2&category_id=1 http//abc.com/newList.php/2/1.html 2 => type=2 1 => category_id=1 */ // /2/1.html if(preg_match('/^\/(\d+)\/(\d+).html'.$_SERVER['PATH_INFO'],$arr)){ $type=$arr[1]; $category_id=$arr[2]; }else{ //TODO } ?>
ii.WEB服務器rewrite配置達到僞靜態
apache下rewrite配置
1.Http.conf文件中開啓相關模式
LoadModule rewrite_module modules/mod_rewrite.so
經過phpinfo();設置是否開啓成功:
Include conf/extra/httpd-vhosts.conf
ii. vhosts文件增長相關域名
Httpd_vhosts.conf配置文件配置相關信息
iii. 僞靜態配置案例
RewriteEngine on //開啓rewrite引擎
RewriteRule^/detail/([0-9]*).html$/detail.php?id=$1 //設置的一個僞靜態規則
state.com/detail/134.html state.com/detail.php?id=134
當服務器項目中存在這個目錄或者這個文件,就去訪問。不然去訪問僞靜態文件。
Nginx下rewrite配置