PHP头条
热点:

FirePHP调试指南


如果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

www.phpzy.comtrue/phprm/11144.htmlTechArticleFirePHP调试指南 如果Web前端调试来说,Firebug是不可或缺好的调试工具,它能够监控网络、监测css、js错误,查看DOM节点,查看当前页面获得了几个A,等等功能。 PHP同样也有配合firebug这...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐