處理PHP中錯誤的方法(2)_PHP教程
推薦:如何使PHP和JS實現HTTP上安全地傳輸密碼1、理論 在普通HTTP上,一般表單中的密碼都是以明文方式傳到服務器進行處理的。這無疑給了壞人以可乘之機!這里我們就說說怎么傳輸密碼才是安全的! 與其傳輸密碼本身,到不如傳輸其加密后的形式。MD5是個不錯的選擇。第一,不同的資源幾乎不可能生成相同的MD5
ok,來看比較oo的處理方式:
class ErrorHandlers extends Exception{
private $_context = null;
function __construct($level, $string, $file, $line, $context=null){
parent::__construct($string,$level);
$this->file = $file;
$this->line = $line;
$this->_level = $level;
$this->_context = $context;
}
function __destruct(){
// parent::__destruct();
}
function Message(){
$errors = array(
E_ERROR => 'error',
E_WARNING => 'warning',
E_PARSE => 'parsing error',
E_NOTICE => 'notice',
E_CORE_ERROR => 'core error',
E_CORE_WARNING => 'core warning',
E_COMPILE_ERROR => 'compile error',
E_COMPILE_WARNING => 'compile warning',
E_USER_ERROR => 'user error',
E_USER_WARNING => 'user warning',
E_USER_NOTICE => 'user notice'
);
$str = $errors[parent::getCode()].': '.parent::getMessage().' 在 '.parent::getFile().
' 的第 '.parent::getLine()."行\n" ;
if($this->_level==E_USER_ERROR){
$str .= ('
致命錯誤');
}
echo('
');
echo($str);
echo('
');
}
}
function error_handler($errno,$errstr,$errorfile,$errline,$errtext){
throw new ErrorHandlers($errno,$errstr,$errorfile,$errline,$errtext);
}
function exception_handler(Exception $e)
{
$errors = array(
E_ERROR => 'error',
E_WARNING => 'warning',
E_PARSE => 'parsing error',
E_NOTICE => 'notice',
E_CORE_ERROR => 'core error',
E_CORE_WARNING => 'core warning',
E_COMPILE_ERROR => 'compile error',
E_COMPILE_WARNING => 'compile warning',
E_USER_ERROR => 'user error',
E_USER_WARNING => 'user warning',
E_USER_NOTICE => 'user notice');
echo $errors[$e->getCode()].': '.$e->getMessage().' in '.$e->getFile().
' on line '.$e->getLine()."\n";
echo $e->getTraceAsString();
}
trigger_error('5do8');
try{
$i = 1/0;
} catch(ErrorHandlers $e) {
echo "發生錯誤."; //可以輸出錯誤行
$e->Message();
}
而后,注意了,如果您第一次(或者重新)加載的話,就加上:
set_error_handler('error_handler');
set_exception_handler('exception_handler');
如果不是上述情況,就不要加了,否則會出現
Exception thrown without a stack frame in Unknown on line 0
因為error_handler是anto_flush的。
在一個exception里面不能調用其他的exception。有2條普遍適用的規則,如下:
1:不要在一個Exception里面執行另一個Exception
2:不要在析構函數里面執行Exception.
restore_exception_handler();是可以保存exception柄的,注意,執行error以后就會有Exception的了。
最后,加上一個完整的例子:CallError.php
error_reporting(1048);
class ErrorHandlers extends Exception{
private $_context = null;
function __construct($level, $string, $file, $line, $context=null){
parent::__construct($string,$level);
$this->file = $file;
$this->line = $line;
$this->_level = $level;
$this->_context = $context;
}
function __destruct(){
// parent::__destruct();
}
function Message(){
$errors = array(
E_ERROR => 'error',
E_WARNING => 'warning',
E_PARSE => 'parsing error',
E_NOTICE => 'notice',
E_CORE_ERROR => 'core error',
E_CORE_WARNING => 'core warning',
E_COMPILE_ERROR => 'compile error',
E_COMPILE_WARNING => 'compile warning',
E_USER_ERROR => 'user error',
E_USER_WARNING => 'user warning',
E_USER_NOTICE => 'user notice'
);
$str = $errors[parent::getCode()].': '.parent::getMessage().' 在 '.parent::getFile().
' 的第 '.parent::getLine()."行\n" ;
if($this->_level==E_USER_ERROR){
$str .= ('
致命錯誤');
}
echo('
');
echo($str);
echo('
');
}
}
function error_handler($errno,$errstr,$errorfile,$errline,$errtext){
throw new ErrorHandlers($errno,$errstr,$errorfile,$errline,$errtext);
}
function exception_handler(Exception $e)
{
$errors = array(
E_ERROR => 'error',
E_WARNING => 'warning',
E_PARSE => 'parsing error',
E_NOTICE => 'notice',
E_CORE_ERROR => 'core error',
E_CORE_WARNING => 'core warning',
E_COMPILE_ERROR => 'compile error',
E_COMPILE_WARNING => 'compile warning',
E_USER_ERROR => 'user error',
E_USER_WARNING => 'user warning',
E_USER_NOTICE => 'user notice');
echo $errors[$e->getCode()].': '.$e->getMessage().'在'.$e->getFile().
'的第'.$e->getLine()."行\n";
echo $e->getMessage();
die();
}
//
set_error_handler('error_handler');
//restore_error_handler();
set_exception_handler('exception_handler');
//restore_exception_handler();
我肯定是錯誤
?>
執行結果:
notice: Use of undefined constant 我肯定是錯誤 - assumed '我肯定是錯誤'在E:\web\web\php\bi\exception\m.php的第74行 Use of undefined constant 我肯定是錯誤 - assumed '我肯定是錯誤'
另外,在類中,還可以這樣:
function trigger_error($error_msg, $error_type = E_USER_WARNING)
{
trigger_error(" error: $error_msg", $error_type);
}
著名的Smarty就是這么做的.
分享:解答PHP上傳多個圖片并校驗的代碼問題單張的圖片上傳是不復雜的,這里涉及到多張圖片上傳和對圖片格式的校驗,保證上傳的一定是圖片,防止上傳其他文件到服務器。基本實現算法是使用數組的形式,把所有的圖片提交個一個數組,對數組的元素進行一個個的處理。 以下為引用的內容: 以下為引用的內
- 相關鏈接:
- 教程說明:
PHP教程-處理PHP中錯誤的方法(2)。