php专区

 首页 > php专区 > PHP应用 > php类库 > PHP系统异常处理类程序

PHP系统异常处理类程序

分享到:
【字体:大 中 小】
导读:
          以前我们用过的异常处理函数都是单个的,下面我找到一个非常的不错的异常处理类系统,不但可以控制错误还能给出好的界面哦. PHP系统异常处理类程序代码如下: ?php //自定义异常函数...

以前我们用过的异常处理函数都是单个的,下面我找到一个非常的不错的异常处理类系统,不但可以控制错误还能给出好的界面哦.

PHP系统异常处理类程序代码如下:

  1.  
  2. // 自定义异常函数 
  3. set_exception_handler('handle_exception'); 
  4.  
  5. // 自定义错误函数 
  6. set_error_handler('handle_error'); 
  7.  
  8. /** 
  9.  * 异常处理 
  10.  * 
  11.  * @param mixed $exception 异常对象 
  12.  * @author www.phpfensi.com 
  13.  */ 
  14. function handle_exception($exception) { 
  15.  Error::exceptionError($exception); 
  16. } 
  17.  
  18. /** 
  19.  * 错误处理 
  20.  * 
  21.  * @param string $errNo 错误代码 
  22.  * @param string $errStr 错误信息 
  23.  * @param string $errFile 出错文件 
  24.  * @param string $errLine 出错行 
  25.  * @author www.phpfensi.com 
  26.  */ 
  27. function handle_error($errNo, $errStr, $errFile, $errLine) { 
  28.  if ($errNo) { 
  29.   Error::systemError($errStr, false, true, false); 
  30.  } 
  31. } 
  32.  
  33. /** 
  34.  * 系统错误处理 
  35.  * 
  36.  * @author www.phpfensi.com 
  37.  */ 
  38. class Error { 
  39.  
  40.  public static function systemError($message, $show = true, $save = true, $halt = true) { 
  41.  
  42.   list($showTrace, $logTrace) = self::debugBacktrace(); 
  43.  
  44.   if ($save) { 
  45.    $messageSave = '' . $message . 'PHP:' . $logTrace; 
  46.    self::writeErrorLog($messageSave); 
  47.   } 
  48.  
  49.   if ($show) { 
  50.    self::showError('system', "
  51. $message
  52. ", $showTrace, 0); 
  53.   } 
  54.  
  55.   if ($halt) { 
  56.    exit(); 
  57.   } else { 
  58.    return $message; 
  59.   } 
  60.  } 
  61.  
  62.  /** 
  63.   * 代码执行过程回溯信息 
  64.   * 
  65.   * @static 
  66.   * @access public 
  67.   */ 
  68.  public static function debugBacktrace() { 
  69.   $skipFunc[] = 'Error->debugBacktrace'; 
  70.  
  71.   $show = $log = ''; 
  72.   $debugBacktrace = debug_backtrace(); 
  73.   ksort($debugBacktrace); 
  74.   foreach ($debugBacktrace as $k => $error) { 
  75.    if (!isset($error['file'])) { 
  76.     // 利用反射API来获取方法/函数所在的文件和行数 
  77.     try { 
  78.      if (isset($error['class'])) { 
  79.       $reflection = new ReflectionMethod($error['class'], $error['function']); 
  80.      } else { 
  81.       $reflection = new ReflectionFunction($error['function']); 
  82.      } 
  83.      $error['file'] = $reflection->getFileName(); 
  84.      $error['line'] = $reflection->getStartLine(); 
  85.     } catch (Exception $e) { 
  86.      continue; 
  87.     } 
  88.    } 
  89.  
  90.    $file = str_replace(SITE_PATH, '', $error['file']); 
  91.    $func = isset($error['class']) ? $error['class'] : ''; 
  92.    $func .= isset($error['type']) ? $error['type'] : ''; 
  93.    $func .= isset($error['function']) ? $error['function'] : ''; 
  94.    if (in_array($func, $skipFunc)) { 
  95.     break; 
  96.    } 
  97.    $error['line'] = sprintf('%04d', $error['line']); 
  98.  
  99.    $show .= '
  100. [Line: ' . $error['line'] . ']' . $file . '(' . $func . ')
  101. '; 
  102.    $log .= !emptyempty($log) ? ' -> ' : ''; 
  103.    $log .= $file . ':' . $error['line']; 
  104.   } 
  105.   return array($show, $log); 
  106.  } 
  107.  
  108.  /** 
  109.   * 异常处理 
  110.   * 
  111.   * @static 
  112.   * @access public 
  113.   * @param mixed $exception 
  114.   */ 
  115.  public static function exceptionError($exception) { 
  116.   if ($exception instanceof DbException) { 
  117.    $type = 'db'; 
  118.   } else { 
  119.    $type = 'system'; 
  120.   } 
  121.   if ($type == 'db') { 
  122.    $errorMsg = '(' . $exception->getCode() . ') '; 
  123.    $errorMsg .= self::sqlClear($exception->getMessage(), $exception->getDbConfig()); 
  124.    if ($exception->getSql()) { 
  125.     $errorMsg .= ''; 
  126.     $errorMsg .= self::sqlClear($exception->getSql(), $exception->getDbConfig()); 
  127.     $errorMsg .= '
'; 
  •    } 
  •   } else { 
  •    $errorMsg = $exception->getMessage(); 
  •   } 
  •   $trace = $exception->getTrace(); 
  •   krsort($trace); 
  •   $trace[] = array('file' => $exception->getFile(), 'line' => $exception->getLine(), 'function' => 'break'); 
  •   $phpMsg = array(); 
  •   foreach ($trace as $error) { 
  •    if (!emptyempty($error['function'])) { 
  •     $fun = ''; 
  •     if (!emptyempty($error['class'])) { 
  •      $fun .= $error['class'] . $error['type']; 
  •     } 
  •     $fun .= $error['function'] . '('; 
  •     if (!emptyempty($error['args'])) { 
  •      $mark = ''; 
  •      foreach ($error['args'] as $arg) { 
  •       $fun .= $mark; 
  •       if (is_array($arg)) { 
  •        $fun .= 'Array'; 
  •       } elseif (is_bool($arg)) { 
  •        $fun .= $arg ? 'true' : 'false'; 
  •       } elseif (is_int($arg)) { 
  •        $fun .= (defined('SITE_DEBUG') && SITE_DEBUG) ? $arg : '%d'; 
  •       } elseif (is_float($arg)) { 
  •        $fun .= (defined('SITE_DEBUG') && SITE_DEBUG) ? $arg : '%f'; 
  •       } else { 
  •        $fun .= (defined('SITE_DEBUG') && SITE_DEBUG) ? ''' . htmlspecialchars(substr(self::clear($arg), 0, 10)) . (strlen($arg) > 10 ? ' ...' : '') . ''' : '%s'; 
  •       } 
  •       $mark = ', '; 
  •      } 
  •     } 
  •     $fun .= ')'; 
  •     $error['function'] = $fun; 
  •    } 
  •    if (!isset($error['line'])) { 
  •     continue; 
  •    } 
  •    $phpMsg[] = array('file' => str_replace(array(SITE_PATH, ''), array('', '/'), $error['file']), 'line' => $error['line'], 'function' => $error['function']); 
  •   } 
  •   self::showError($type, $errorMsg, $phpMsg); 
  •   exit(); 
  •  } 
  •  
  •  /** 
  •   * 记录错误日志 
  •   * 
  •   * @static 
  •   * @access public 
  •   * @param string $message 
  •   */ 
  •  public static function writeErrorLog($message) { 
  •  
  •   return false; // 暂时不写入 www.phpfensi.com 
  •  
  •   $message = self::clear($message); 
  •   $time = time(); 
  •   $file = LOG_PATH . '/' . date('Y.m.d') . '_errorlog.php'; 
  •   $hash = md5($message); 
  •  
  •   $userId = 0; 
  •   $ip = get_client_ip(); 
  •  
  •   $user = 'User: userId=' . intval($userId) . '; IP=' . $ip . '; RIP:' . $_SERVER['REMOTE_ADDR']; 
  •   $uri = 'Request: ' . htmlspecialchars(self::clear($_SERVER['REQUEST_URI'])); 
  •   $message = " {$time} $message $hash $user $uri "; 
  •  
  •   // 判断该$message是否在时间间隔$maxtime内已记录过,有,则不用再记录了 
  •   if (is_file($file)) { 
  •    $fp = @fopen($file, 'rb'); 
  •    $lastlen = 50000;  // 读取最后的 $lastlen 长度字节内容 
  •    $maxtime = 60 * 10;  // 时间间隔:10分钟 
  •    $offset = filesize($file) - $lastlen; 
  •    if ($offset > 0) { 
  •     fseek($fp, $offset); 
  •    } 
  •    if ($data = fread($fp, $lastlen)) { 
  •     $array = explode(" ", $data); 
  •     if (is_array($array)) 
  •      foreach ($array as $key => $val) { 
  •       $row = explode(" ", $val); 
  •       if ($row[0] != '') { 
  •        continue; 
  •       } 
  •       if ($row[3] == $hash && ($row[1] > $time - $maxtime)) { 
  •        return; 
  •       } 
  •      } 
  •    } 
  •   } 
  •  
  •   error_log($message, 3, $file); 
  •  } 
  •  
  •  /** 
  •   * 清除文本部分字符 
  •   * 
  •   * @param string $message 
  •   */ 
  •  public static function clear($message) { 
  •   return str_replace(array(" ", " ", " "), " ", $message); 
  •  } 
  •  
  •  /** 
  •   * sql语句字符清理 
  •   * 
  •   * @static 
  •   * @access public 
  •   * @param string $message 
  •   * @param string $dbConfig 
  •   */ 
  •  public static function sqlClear($message, $dbConfig) { 
  •   $message = self::clear($message); 
  •   if (!(defined('SITE_DEBUG') && SITE_DEBUG)) { 
  •    $message = str_replace($dbConfig['database'], '***', $message); 
  •    //$message = str_replace($dbConfig['prefix'], '***', $message); 
  •    $message = str_replace(C('DB_PREFIX'), '***', $message); 
  •   } 
  •   $message = htmlspecialchars($message); 
  •   return $message; 
  •  } 
  •  
  •  /** 
  •   * 显示错误 
  •   * 
  •   * @static 
  •   * @access public 
  •   * @param string $type 错误类型 db,system 
  •   * @param string $errorMsg 
  •   * @param string $phpMsg 
  •   */ 
  •  public static function showError($type, $errorMsg, $phpMsg = '') { 
  •   global $_G; 
  •  
  •   $errorMsg = str_replace(SITE_PATH, '', $errorMsg); 
  •   ob_end_clean(); 
  •   $host = $_SERVER['HTTP_HOST']; 
  •   $title = $type == 'db' ? 'Database' : 'System'; 
  •   echo <<
  • "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  •  
  •  
  •  <span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$host</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> - </span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$title</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Error 
  •  "Content-Type" content="text/html; charset={$_G['config']['output']['charset']}" /> 
  •  "ROBOTS" content="NOINDEX,NOFOLLOW,NOARCHIVE" /> 
  •  "text/css"> 
  •   
  •   
  •  
  •  
  • "container"> 
  • $title Error

     
  • class='info'>$errorMsg
  •  
  • EOT; 
  •   if (!emptyempty($phpMsg)) { 
  •    echo ''; 
  •    echo '

    PHP Debug

    '
    ; 
  •    echo ''; 
  •    if (is_array($phpMsg)) { 
  •     echo 'No.FileLineCode'; 
  •     foreach ($phpMsg as $k => $msg) { 
  •      $k++; 
  •      echo ''; 
  •      echo '' . $k . ''; 
  •      echo '' . $msg['file'] . ''; 
  •      echo '' . $msg['line'] . ''; 
  •      echo '' . $msg['function'] . ''; 
  •      echo ''; 
  •     } 
  •    } else { 
  •     echo '
      ' . $phpMsg . '
    '
    ; 
  •    } 
  •    echo '
  • '; 
  •   } 
  •   echo <<
  •  
  •  
  •  
  • EOT; 
  •   exit(); 
  •  } 
  • } 
  •  
  • /** 
  •  * DB异常类 
  •  * 
  •  * @author www.phpfensi.com 
  •  */ 
  • class DbException extends Exception { 
  •  
  •  protected $sql; 
  •  protected $dbConfig; // 当前数据库配置信息 
  •  
  •  public function __construct($message, $code = 0, $sql = '', $dbConfig = array()) { 
  •   $this->sql = $sql; 
  •   $this->dbConfig = $dbConfig; 
  •   parent::__construct($message, $code); 
  •  }//开源代码phpfensi.com 
  •  
  •  public function getSql() { 
  •   return $this->sql; 
  •  } 
  •  
  •  public function getDbConfig() { 
  •   return $this->dbConfig; 
  •  } 
  • } 
  • ?>
  •  
  • 分享到:
    PHP文件操作类(文件和文件夹创建,复制...
    PHP文件操作类(文件和文件夹创建,复制,移动和删除) 本文章给大家分享的文件操作类包括对文件和文件夹创建,复制,移动和删除,有需要对文件操作学习的同学可进入参考参考。 实例代码如下:  
    微信公众号开发之微信公共平台消息回复类
    微信公众号开发代码我在网上看到了有不少,其实都是大同小义了都是参考官方给出的demo文件进行修改的,下面给各位分享一个. 初次接触的时候写的一些,有点乱…也没去整理…最近都不想工作了,各种烦,午饭也没吃,就是想表达一下我过的不好,–请忽略个人情绪往下看,代码如下:
    •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
    • 在这里……