如果Web前端調試來說,Firebug是不可或缺好的調試工具,它能夠監控網絡、監測css、js錯誤,查看DOM節點,查看當前頁面獲得了幾個A,等等功能。
PHP同樣也有配合firebug這麼好用的工具,那就是FirePHP。
FirePHP是一個附加在 firebug 上面的插件,用來調試PHP,操作過程很簡單。在PHP端使用FirePHP提供的PHP日志記錄類庫來輸出調試信息, 在浏覽器端使用 Firebug + FirePHP 來接收查看輸出的調試信息,這些調試信息會直接附加在返回的HTTP頭信息裡,這些信息不會被浏覽器直接顯示,只會在firephp 獲取顯示,有效的達到了調試和頁面顯示都不沖突的問題。(必須使用firefox浏覽器)
FirePHP調試原理
通過服務端庫FirePHPCore,和基於Firebug的插件FirePHP,PHP腳本可以通過HTTP請求頭發送調試信息到浏覽器。一旦你設置開啟FirePHP,你可以在Firebug的控制台獲得PHP腳本警告和錯誤,就感覺像直接調試JavaScript一樣。
安裝
首先需要安裝Firefox,Firebug,FirePHP,建議使用最新版本FireFox 19.0.2,Firebug 1.11.2,FirePHP 0.7.1;
其次下載服務器端庫FirePHPCore:
> wget http://www.firephp.org/DownloadRelease/FirePHPLibrary-FirePHPCore-0.3.2
> file FirePHPLibrary-FirePHPCore-0.3.2
FirePHPLibrary-FirePHPCore-0.3.2: Zip archive data, at least v2.0 to extract
> unzip FirePHPLibrary-FirePHPCore-0.3.2
~/public_html/FirePHPCore-0.3.2> ls -R
.:
CHANGELOG CREDITS FirePHPCore-0.3.2 FirePHPLibrary-FirePHPCore-0.3.2 lib README test
./FirePHPCore-0.3.2:
CHANGELOG CREDITS lib README
./FirePHPCore-0.3.2/lib:
FirePHPCore
./FirePHPCore-0.3.2/lib/FirePHPCore: # 對於PHP5+只需用於fb.php,FirePHP.class.php這兩個文件
fb.php fb.php4 FirePHP.class.php FirePHP.class.php4 LICENSE
./lib:
FirePHPCore
./lib/FirePHPCore:
fb.php fb.php4 FirePHP.class.php FirePHP.class.php4 LICENSE
./test: # 自已在解壓縮目錄下創建test目錄用於測試FirePHP
firephptest.php測試
接下來 在test目錄下創建測試用例firephptest.php:
<?php
require_once '../lib/FirePHPCore/fb.php';
$firephp = FirePHP::getInstance(true);
$var = array(1, 2, 'hello world', array(1));
fb($var);
fb($var, 'Label');最後在浏覽器訪問www.domain.com/~zhanhailiang/FirePHPCore-0.3.2/test/firephptest.php,可以在控制台上看到如下輸出:
http://itravel.smartcom.cc/~zhanhailiang/FirePHPCore-0.3.2/test/firephptest.php
log: array('0'=>'1', '1'=>'2', '2'=> ... )
log: Label: array('0'=>'1', '1'=>'2', '2'=> ... )通過Firebug網絡面板可以看到HTTP請求的響應頭詳情:
Connection close
Content-Encoding gzip
Content-Type text/html; charset=utf-8
Date Fri, 29 Mar 2013 01:39:32 GMT
Server nginx
Transfer-Encoding chunked
X-Wf-1-1-1-1 142|[{"Type":"LOG","File":"\/home\/zhanhailiang\/public_html\/FirePHPCore-0.3.2\/test\/firephptest.php","Line":"8"},["1","2","hello world",["1"]]]|
X-Wf-1-1-1-2 158|[{"Type":"LOG","Label":"Label","File":"\/home\/zhanhailiang\/public_html\/FirePHPCore-0.3.2\/test\/firephptest.php","Line":"9"},["1","2","hello world",["1"]]]|
X-Wf-1-Index 2
X-Wf-1-Plugin-1 http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3
X-Wf-1-Structure-1 http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1
X-Wf-Protocol-1 http://meta.wildfirehq.org/Protocol/JsonStream/0.2通過分析FirePHP開啟和關閉選項,可以發現當FirePHP關閉時響應信息裡是不存在X-Wf-***。通過分析HTTP請求可以發現,
當關閉FirePHP,HTTP請求頭為:
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control max-age=0
Connection keep-alive
Host itravel.smartcom.cc
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0當開啟FirePHP時,HTTP請求頭為:
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control no-cache
Connection keep-alive
Host itravel.smartcom.cc
Pragma no-cache
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.7.1
x-insight activate可以看出開啟FirePHP時HTTP請求頭區別在於:
Pragma no-cache
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.7.1
x-insight activate通過查看FirePHP.class.php源碼可以看到FirePHPCore通過UA來判斷客戶端是否安裝FirePHP:
/**
* Check if FirePHP is installed on client
*
* @return boolean
*/
public function detectClientExtension()
{
// Check if FirePHP is installed on client via User-Agent header
if (@preg_match_all('/\sFirePHP\/([\.\d]*)\s?/si',$this->getUserAgent(),$m) &&
version_compare($m[1][0],'0.0.6','>=')) {
return true;
} else
// Check if FirePHP is installed on client via X-FirePHP-Version header
if (@preg_match_all('/^([\.\d]*)$/si',$this->getRequestHeader("X-FirePHP-Version"),$m) &&
version_compare($m[1][0],'0.0.6','>=')) {
return true;
}
return false;
}那麼x-insight請求頭有沒有用呢?查看FirePHPCore裡的所有代碼,並沒有看到x-insight的用途,所以我猜測x-insight並沒有實際用途。為了驗證我的猜測,使用FF擴展HTTPRequester工具模擬構造一個只有UA:FirePHP,而沒有x-insight請求頭,測試下是否響應頭和包含x-insight請求的響應頭是否一致:
GET http://itravel.smartcom.cc/~zhanhailiang/FirePHPCore-0.3.2/test/firephptest.php
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.7.1
-- response --
200 OK
Server: nginx
Date: Fri, 29 Mar 2013 02:34:54 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
X-Wf-1-1-1-1: 142|[{"Type":"LOG","File":"\/home\/zhanhailiang\/public_html\/FirePHPCore-0.3.2\/test\/firephptest.php","Line":"8"},["1","2","hello world",["1"]]]|
X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2
X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3
X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1
X-Wf-1-1-1-2: 158|[{"Type":"LOG","Label":"Label","File":"\/home\/zhanhailiang\/public_html\/FirePHPCore-0.3.2\/test\/firephptest.php","Line":"9"},["1","2","hello world",["1"]]]|
X-Wf-1-Index: 2
Content-Encoding: gzip