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.  * @param string $errNo 错误代码 
  21.  * @param string $errStr 错误信息 
  22.  * @param string $errFile 出错文件 
  23.  * @param string $errLine 出错行 
  24.  * @author www.phpfensi.com 
  25.  */ 
  26. function handle_error($errNo$errStr$errFile$errLine) { 
  27.  if ($errNo) { 
  28.   Error::systemError($errStr, false, true, false); 
  29.  } 
  30.  
  31. /** 
  32.  * 系统错误处理 
  33.  * 
  34.  * @author www.phpfensi.com 
  35.  */ 
  36. class Error { 
  37.  
  38.  public static function systemError($message$show = true, $save = true, $halt = true) { 
  39.  
  40.   list($showTrace$logTrace) = self::debugBacktrace(); 
  41.  
  42.   if ($save) { 
  43.    $messageSave = '' . $message . 'PHP:' . $logTrace
  44.    self::writeErrorLog($messageSave); 
  45.   } 
  46.  
  47.   if ($show) { 
  48.    self::showError('system'"
  49. $message
  50. "$showTrace, 0); 
  51.   } 
  52.  
  53.   if ($halt) { 
  54.    exit(); 
  55.   } else { 
  56.    return $message
  57.   } 
  58.  } 
  59.  
  60.  /** 
  61.   * 代码执行过程回溯信息 
  62.   * 
  63.   * @static 
  64.   * @access public 
  65.   */ 
  66.  public static function debugBacktrace() { 
  67.   $skipFunc[] = 'Error->debugBacktrace'
  68.  
  69.   $show = $log = ''
  70.   $debugBacktrace = debug_backtrace(); 
  71.   ksort($debugBacktrace); 
  72.   foreach ($debugBacktrace as $k => $error) { 
  73.    if (!isset($error['file'])) { 
  74.     // 利用反射API来获取方法/函数所在的文件和行数 
  75.     try { 
  76.      if (isset($error['class'])) { 
  77.       $reflection = new ReflectionMethod($error['class'], $error['function']); 
  78.      } else { 
  79.       $reflection = new ReflectionFunction($error['function']); 
  80.      } 
  81.      $error['file'] = $reflection->getFileName(); 
  82.      $error['line'] = $reflection->getStartLine(); 
  83.     } catch (Exception $e) { 
  84.      continue
  85.     } 
  86.    } 
  87.  
  88.    $file = str_replace(SITE_PATH, ''$error['file']); 
  89.    $func = isset($error['class']) ? $error['class'] : ''
  90.    $func .= isset($error['type']) ? $error['type'] : ''
  91.    $func .= isset($error['function']) ? $error['function'] : ''
  92.    if (in_array($func$skipFunc)) { 
  93.     break
  94.    } 
  95.    $error['line'] = sprintf('%04d'$error['line']); 
  96.  
  97.    $show .= '
  98. [Line: ' . $error['line'] . ']' . $file . '(' . $func . ')
  99. '
  100.    $log .= !emptyempty($log) ? ' -> ' : ''
  101.    $log .= $file . ':' . $error['line']; 
  102.   } 
  103.   return array($show$log); 
  104.  } 
  105.  
  106.  /** 
  107.   * 异常处理 
  108.   * 
  109.   * @static 
  110.   * @access public 
  111.   * @param mixed $exception 
  112.   */ 
  113.  public static function exceptionError($exception) { 
  114.   if ($exception instanceof DbException) { 
  115.    $type = 'db'
  116.   } else { 
  117.    $type = 'system'
  118.   } 
  119.   if ($type == 'db') { 
  120.    $errorMsg = '(' . $exception->getCode() . ') '
  121.    $errorMsg .= self::sqlClear($exception->getMessage(), $exception->getDbConfig()); 
  122.    if ($exception->getSql()) { 
  123.     $errorMsg .= ''
  124.     $errorMsg .= self::sqlClear($exception->getSql(), $exception->getDbConfig()); 
  125.     $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技术的分享社区,专属您自己的技术摘抄本、收藏夹。
    • 在这里……