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

WBOY
Release: 2016-06-20 12:47:40
Original
916 people have browsed it

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

默认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");
Copy after login


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>
Copy after login

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!