Home  >  Article  >  Backend Development  >  【PHP错误捕获分享】PHP捕获应用错误 简单记录并友好处理展示

【PHP错误捕获分享】PHP捕获应用错误 简单记录并友好处理展示

WBOY
WBOYOriginal
2016-06-20 12:47:40964browse

为了某种目的而需要捕获系统错误,在此简单分享下某个项目中的捕获方法。

默认php错误如图

index.php

/** * 捕获应用错误记录 */function ef_error_record($errno,$errstr,$errfile,$errline){	$e = array("no"=>$errno,"str"=>$errstr,"file"=>$errfile,"line"=>$errline);        ef_error_display("record",$e);}/** * 错误中断显示 */function ef_error_display($method='',$option=''){	static $e = array();	if($method=='record'){		$e[] =  $option;		return true;	}else if(!empty($e)){		include ('/common/template/error.php');		exit();	}	return true;}                        //禁止错误输出        error_reporting(0);        //设置自定义错误函数 捕获系统错误并记录	set_error_handler("ef_error_record");	//注册结束脚本函数 该函数会有意外或正常结束脚本触发(同析构函数)	register_shutdown_function("ef_error_display");


error.php

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>EFrame 运行错误</title><style type="text/css">body {background-color:	#fff;margin:				40px;font-family:		Lucida Grande, Verdana, Sans-serif;font-size:			12px;color:				#000;}#content  {border:				#999 1px solid;background-color:	#fff;padding:			20px 20px 12px 20px;}h1 {font-weight:		normal;font-size:			16px;color:				#990000;margin: 			0 0 4px 0;}span {color:				#990000;	}.msg p{	color:#990000;}.file{	text-indent:15px;;}</style></head><body>	<div id="content">		<h1>EFrame 运行错误</h1>		<?php			foreach($e as $key=>$val){				echo "<div class=\"msg\"><p><span>[".($key+1)."]</span> {$val['str']} </p></div>",				"<div class=\"file\"><p>".$val['file']." 行: <span>{$val['line']}</span></p></div>";			}			?>	</div></body></html>

最终错误错误页面(当然页面可以自己修改想要的样式):

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