【PHP錯誤捕獲分享】PHP捕獲應用錯誤 簡單記錄並友好處理展現

爲了某種目的而須要捕獲系統錯誤,在此簡單分享下某個項目中的捕獲方法。php

默認php錯誤如圖css

index.phphtml

/**
 * 捕獲應用錯誤記錄
 */
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>

最終錯誤錯誤頁面(固然頁面能夠本身修改想要的樣式):
ui

相關文章
相關標籤/搜索