Home >php教程 >php手册 >PHP set_error_handler()函数的使用,phpseterrorhandler

PHP set_error_handler()函数的使用,phpseterrorhandler

WBOY
WBOYOriginal
2016-06-13 08:45:05890browse

PHP set_error_handler()函数的使用,phpseterrorhandler

我们写程序,难免会有问题(是经常会遇到问题 ),而PHP遇到错误时,就会给出出错脚本的位置、行数和原因。有很多人说,这并没有什么大不了。确实,在调试程序阶段,这确实是没啥的,而且我认为给出错误路径是必要的。

但泄露了实际路径的后果是不堪设想的,对于某些入侵者,这个信息可是非常重要,而事实上现在有很多的服务器都存在这个问题。有些网管干脆把PHP配置文件中的display_errors设置为Off来解决(貌似我们就是这样做的),但本人认为这个方法过于消极。

有些时候,我们的确需要PHP返回错误的信息以便调试。而且在出错时也可能需要给用户一个交待,甚至导航到另一页面。

那么,有啥解决办法呢?

set_error_handler()

PHP从4.1.0开始提供了自定义错误处理句柄的功能函数set_error_handler(),但很少数脚本编写者知道。set_error_handler这个函数可以很好地防止错误路径泄露,当然还有其它更多的作用。

set_error_handler的使用方法如下:

<span>string</span> set_error_handler ( callback error_handler [, <span>int</span> error_types])

现在我们就用自定义的错误处理把实际路径过滤掉。假设有一个变量$admin,我们是用来判断访问者是否是管理员的(可以通过IP或者登录的用户id来做这个判断)

<span>//</span><span>admin为管理员的身份判定,true为管理员。 
</span><span>//</span><span>自定义的错误处理函数一定要有这4个输入变量$errno,$errstr,$errfile,$errline,否则无效。</span>
<span>
function my_error_handler($errno,$errstr,$errfile,$errline) 
{ 
     </span><span>//</span><span>如果不是管理员就过滤实际路径 </span>
    <span>if</span>(!<span>admin) 
    { 
        $errfile</span>=str_replace(getcwd(),<span>""</span><span>,$errfile); 
        $errstr</span>=str_replace(getcwd(),<span>""</span><span>,$errstr); 
    } 
     </span><span>switch</span><span>($errno) 
    { 
        </span><span>case</span><span> E_ERROR: 
          echo </span><span>"</span><span>ERROR: [ID $errno] $errstr (Line: $errline of $errfile) \n</span><span>"</span><span>; 
        echo </span><span>"</span><span>程序已经停止运行,请联系管理员。</span><span>"</span><span>; 
        </span><span>//</span><span>遇到Error级错误时退出脚本 </span>
        <span>break</span><span>; 
        </span><span>case</span><span> E_WARNING: 
        echo </span><span>"</span><span>WARNING: [ID $errno] $errstr (Line: $errline of $errfile) \n</span><span>"</span><span>; 
        </span><span>break</span><span>; 
        </span><span>default</span><span>: 
        </span><span>//</span><span>不显示Notice级的错误 </span>
        <span>break</span><span>; 
        } 
}  </span>

这样就自定义了一个错误处理函数,那么怎么把错误的处理交给这个自定义函数呢?

<span>//</span><span> 应用到类 </span>
set_error_handler(array(&$<span>this</span>,<span>"</span><span>appError</span><span>"</span><span>)); 
</span><span>//</span><span>示例的做法 </span>
set_error_handler(<span>"</span><span>my_error_handler</span><span>"</span>);  

so easy,这样,就可以很好地解决安全和调试方便的矛盾了。而且你还可以花点心思,使错误提示更加美观以配合网站的风格。

原作者给出了两点需要注意的地方,我也放出来吧,希望引起广大同胞们的注意:

<span>//</span><span>先定义一个函数,也可以定义在其他的文件中,再用require()调用  </span>
<span>function myErrorHandler($errno, $errstr, $errfile, $errline)  
{  
     </span><span>//</span><span>为了安全起见,不暴露出真实物理路径,下面两行过滤实际路径  </span>
    $errfile=str_replace(getcwd(),<span>""</span><span>,$errfile);  
    $errstr</span>=str_replace(getcwd(),<span>""</span><span>,$errstr);  
  
    </span><span>switch</span><span> ($errno) {  
    </span><span>case</span><span> E_USER_ERROR:  
  
     echo </span><span>"</span><span><b>My ERROR</b> [$errno] $errstr<br />\n</span><span>"</span><span>;  
        echo </span><span>"</span><span>  Fatal error on line $errline in file $errfile</span><span>"</span><span>;  
        echo </span><span>"</span><span>, PHP </span><span>"</span> . PHP_VERSION . <span>"</span><span> (</span><span>"</span> . PHP_OS . <span>"</span><span>)<br />\n</span><span>"</span><span>;  
        echo </span><span>"</span><span>Aborting...<br />\n</span><span>"</span><span>;  
        exit(</span><span>1</span><span>);  
        </span><span>break</span><span>;  
  
    </span><span>case</span><span> E_USER_WARNING:  
        echo </span><span>"</span><span><b>My WARNING</b> [$errno] $errstr<br />\n</span><span>"</span><span>;  
        </span><span>break</span><span>;  
  
    </span><span>case</span><span> E_USER_NOTICE:  
        echo </span><span>"</span><span><b>My NOTICE</b> [$errno] $errstr<br />\n</span><span>"</span><span>;  
        </span><span>break</span><span>;  
  
    </span><span>default</span><span>:  
        echo </span><span>"</span><span>Unknown error type: [$errno] $errstr<br />\n</span><span>"</span><span>;  
        </span><span>break</span><span>;  
    }  
  
    </span><span>/*</span><span> Don't execute PHP internal error handler </span><span>*/</span>  
    <span>return</span> <span>true</span><span>;  
}  
  
</span><span>//</span><span>下面开始连接MYSQL服务器,我们故意指定MYSQL端口为3333,实际为3306。  </span>
$link_id=@mysql_pconnect(<span>"</span><span>localhost:3333</span><span>"</span>,<span>"</span><span>root</span><span>"</span>,<span>"</span><span>password</span><span>"</span><span>);  
set_error_handler(myErrorHandler);  
</span><span>if</span> (!<span>$link_id) {  
    trigger_error(</span><span>"</span><span>出错了</span><span>"</span><span>, E_USER_ERROR);  
}  </span>

好了,总结一下,下面是 set_error_handler 三种用法:

<span>class</span><span> CallbackClass {  
   function CallbackFunction() {  
       </span><span>//</span><span> refers to $this  </span>
<span>   }  
  
   function StaticFunction() {  
       </span><span>//</span><span> doesn't refer to $this  </span>
<span>   }  
}  
  
function NonClassFunction($errno, $errstr, $errfile, $errline) {  
}  
  
</span><span>//</span><span> 三种方法如下:  </span>
  
<span>1</span>: set_error_handler(<span>'</span><span>NonClassFunction</span><span>'</span>);  <span>//</span><span> 直接转到一个普通的函数 NonClassFunction  </span>
  
<span>2</span>: set_error_handler(array(<span>'</span><span>CallbackClass</span><span>'</span>, <span>'</span><span>StaticFunction</span><span>'</span>)); <span>//</span><span> 转到 CallbackClass 类下的静方法 StaticFunction  </span>
  
<span>3</span>: $o =& <span>new</span><span> CallbackClass();  
    set_error_handler(array($o, </span><span>'</span><span>CallbackFunction</span><span>'</span>));  <span>//</span><span> 转到类的构造函数,其实本质上跟下面的第四条一样。  </span>
  
<span>4</span>. $o = <span>new</span><span> CallbackClass();  
  
  
</span><span>//</span><span> The following may also prove useful:  </span>
  
<span>class</span><span> CallbackClass {  
   function CallbackClass() {  
       set_error_handler(array(</span>&$<span>this</span>, <span>'</span><span>CallbackFunction</span><span>'</span>)); <span>//</span><span> the & is important  </span>
<span>   }  
     
   function CallbackFunction() {  
       </span><span>//</span><span> refers to $this  </span>
<span>   }  
}  </span>

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn