日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

PHP錯誤處理的函數(shù)和相關(guān)信息

 Ralf_Jones 2006-08-09
配置說明:

 出錯處理和日志

error_reporting  =  E_ALL & ~E_NOTICE & ~E_STRICT
[recommended]->error_reporting  =  E_ALL
 顯示所有的錯誤,除了提醒和編碼標(biāo)準(zhǔn)化警告。
 錯誤報告是位字段。可以將數(shù)字加起來得到想要的錯誤報告等級。
 E_ALL             - 所有的錯誤和警告(不包括 E_STRICT)
 E_ERROR           - 致命性的運行時錯誤
 E_WARNING         - 運行時警告(非致命性錯誤)
 E_PARSE           - 編譯時解析錯誤
 E_NOTICE          - 運行時提醒(這些經(jīng)常是你代碼中的bug引起的,也可能是有意的行為造成的。)
 E_STRICT     - 編碼標(biāo)準(zhǔn)化警告,允許PHP建議如何修改代碼以確保最佳的互操作性向前兼容性。
 E_CORE_ERROR      - PHP啟動時初始化過程中的致命錯誤
 E_CORE_WARNING    - PHP啟動時初始化過程中的警告(非致命性錯)
 E_COMPILE_ERROR   - 編譯時致命性錯
 E_COMPILE_WARNING - 編譯時警告(非致命性錯)
 E_USER_ERROR      - 用戶自定義的錯誤消息
 E_USER_WARNING    - 用戶自定義的警告消息
 E_USER_NOTICE     - 用戶自定義的提醒消息

以上的值(不管是數(shù)字還是符號)被用來建立指定要報告的錯誤的位掩碼(bitmask)。您可以使用位操作符來疊加或者掩去某一種類型的錯誤。請注意 php.ini 僅能識別“|”、“~”、“!”和“&”四種符號,而 php3.ini 無法識別任何位操作符。

在 PHP 4 中,error_reporting 默認的值為 E_ALL & ~E_NOTICE,表示顯示除 E_NOTICE-level 以外所有錯誤的警告信息。在 PHP 3 中,其默認值為 (E_ERROR | E_WARNING | E_PARSE),表示的內(nèi)容和 PHP 4 相同。注意,由于 php3.ini 不支持常量,error_reporting 的設(shè)置應(yīng)該用數(shù)字來表示,因此,其值應(yīng)該為 7。

這項設(shè)置的初始設(shè)定可以通過更改 ini 文件中的 error_reporting 選項來改變,也可以在 Apache 系統(tǒng)中設(shè)置 httpd.conf 中的 php_error_reporting(PHP 3 為 php3_error_reporting)來改變。另外,腳本的運行過程中,也可以通過 error_reporting() 函數(shù)來更改該設(shè)置。

警告

在將服務(wù)端的代碼從 PHP 3 升級到 PHP 4 時,您需要檢查這些設(shè)置,或者調(diào)用 error_reporting(),否則您可能會屏蔽新類型的錯誤報告,尤其是 E_COMPILE_ERROR。這將有可能導(dǎo)致沒有任何反饋信息的空文檔,使得您不知道發(fā)生了什么,也不知道到哪里檢查發(fā)生的問題。

所有的 PHP 表達式都可以使用“@”前綴,該前綴將屏蔽該表達式所有的錯誤信息。如果在該語句運行過程中出現(xiàn)了錯誤并且 track_errors 選項開啟,則您可以在全局變量 $php_errormsg 中找到錯誤信息。

注: @ 錯誤信息控制符前綴不會屏蔽解析錯誤信息。

警告

目前 @ 錯誤信息控制符前綴有可能會屏蔽導(dǎo)致腳本停止運行的關(guān)鍵錯誤的信息。這也就是說,如果您在不可用的,或者未正確定義的函數(shù)前面使用 @ 符號來抑制錯誤,腳本程序會在該處停止運行,并且不會給出任何關(guān)于為什么停止的提示信息。



 例子:
error_reporting = E_ALL & ~E_NOTICE              顯示所有的錯誤,除了提醒
error_reporting = E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR   僅顯示錯誤
display_errors = On
[recommended]->display_errors = Off
 作為輸出的一部分顯示出錯誤信息
 在最終發(fā)布的web站點上,強烈建議你關(guān)掉這個特性,并使用錯誤日志代替(參看下面)。
 在最終發(fā)布的web站點打開這個特性可能暴露一些安全信息,
 例如你的web服務(wù)上的文件路徑、數(shù)據(jù)庫規(guī)劃或別的信息。
display_startup_errors = Off
 即使"display_errors"特性被打開,關(guān)閉此參數(shù)也將不顯示PHP啟動時的錯誤。
 強烈建議你關(guān)掉這個特性,除非你必須要用于調(diào)試中。
log_errors = Off
[recommended]->log_errors = On
 在日志文件里記錄錯誤(服務(wù)器指定的日志,stderr標(biāo)準(zhǔn)錯誤輸出,或error_log(下面的))
 正如上面說明的那樣,強烈建議你在最終發(fā)布的web站點時用日志記錄錯誤而不是直接輸出。
log_errors_max_len = 1024
 設(shè)置錯誤日志文件的最大長度。
 設(shè)為 0 可以允許無限長度。
ignore_repeated_errors = Off
 忽略重復(fù)的錯誤信息
 如果關(guān)閉這個參數(shù),錯誤信息必須出現(xiàn)在同一個文件的同一行才被忽略。
 如果打開這個參數(shù),則沒有這個限制。
ignore_repeated_source = Off
 忽略重復(fù)的錯誤源
 忽略重復(fù)的錯誤信息時忽略重復(fù)的錯誤源
 如果打開這個參數(shù),將不會記錄不同的出錯文件和對應(yīng)行的錯誤信息。
report_memleaks = On
 報告內(nèi)存泄漏
 如果關(guān)閉該參數(shù),將不記錄或者顯示內(nèi)存泄漏
 這個參數(shù)只在調(diào)試編譯中起作用,并且必須在錯誤報告中包含 E_WARNING
track_errors = Off
 保存最近一個錯誤/警告消息于變量$php_errormsg (boolean)中
html_errors = Off
 禁用出錯信息中的HTML標(biāo)記
 注意: 不要在發(fā)布的程序中使用這個特性。
docref_root = "/phpmanual/"
docref_ext = .html
 如果打開了html_errors參數(shù),PHP將會在出錯信息上顯示超連接,
 直接鏈接到一個說明這個錯誤或者導(dǎo)致這個錯誤的函數(shù)的頁面。
 你可以從http://www./docs.php下載php手冊,并設(shè)置docref_root參數(shù),將他指向你本地的手冊所在目錄。
 你還必須設(shè)置"docref_ext"來指定文件的擴展名。
 注意: 不要在發(fā)布的程序中使用這個特性。
error_prepend_string = "<font color=ff0000>"
 用于錯誤信息前輸出的字符串
error_append_string = "</font>"
 用于錯誤信息后輸出的字符串
error_log = filename
 將錯誤日志記錄到哪個文件中。
 如果設(shè)為"syslog", 錯誤日志將被記錄到系統(tǒng)日志中(NT下的事件日志, Unix下的syslog(3))

相關(guān)函數(shù):
debug_backtrace -- Generates a backtrace 創(chuàng)建一個跟蹤調(diào)用傳遞的路線 (PHP5)(PEAR 上PHP4的實現(xiàn))
error_log -- Send an error message somewhere 把錯誤信息寫入日志
error_reporting -- Sets which PHP errors are reported 設(shè)置錯誤報告等級
restore_error_handler -- Restores the previous error handler function 恢復(fù)上一個錯誤處理函數(shù)
restore_exception_handler --  Restores the previously defined exception handler function 恢復(fù)上一個異常處理函數(shù) (PHP 5)
set_error_handler --  Sets a user-defined error handler function 設(shè)置用戶自定義的錯誤處理函數(shù)
set_exception_handler --  Sets a user-defined exception handler function 設(shè)置用戶自定義的異常處理函數(shù)
trigger_error -- Generates a user-level error/warning/notice message 創(chuàng)建一個用戶級別(E_USER開頭)的錯誤信息
user_error -- Alias of trigger_error() trigger_error() 的別名
 
函數(shù)用法:

error_log

送出一個錯誤信息。

語法: int error_log(string message, int message_type, string [destination], string [extra_headers]);

返回值: 整數(shù)

函數(shù)種類: PHP 系統(tǒng)功能

內(nèi)容說明

此函數(shù)會送出錯誤信息到 Web 服務(wù)器的錯誤 log 文件,TCP Port,或到指定文件。第一個參數(shù) message 即為要送出的錯誤信息。第二個參數(shù) message_type 為整數(shù)值:0 表示送到操作系統(tǒng)的 log (UNIX 在 syslog、Windows NT 記錄到事件記錄);1 則使用 PHP 的 Mail() 函數(shù),送信息到某 E-Mail 處,第四個參數(shù) extra_headers 亦會用到;2 則將錯誤信息送到 TCP 埠,此時第三個參數(shù) destination 表示目的地 IP 及 Port;3 則將信息存到文件 destination 中。

使用范例

本例為登入 Oracle 數(shù)據(jù)庫出現(xiàn)問題的處理。

<?php
if (!Ora_Logon($username$password)) {
  
error_log("Oracle 數(shù)據(jù)庫不可用!"0);
}
if (!(
$foo allocate_new_foo()) {
  
error_log("出現(xiàn)大麻煩了!"1"webmaster@www.mydomain.com.tw");
}
error_log("搞砸了!"2"127.0.0.1:9700");
error_log("搞砸了!"2"loghost");
error_log("搞砸了!"3"/var/tmp/my-errors.log");
----------------------------------------------------------------------------------------------------------

error_reporting

設(shè)定錯誤訊息回報的等級。

語法: int error_reporting(int [level]);

傳回值: 整數(shù)

函式種類: PHP 系統(tǒng)功能

內(nèi)容說明

本函式用來設(shè)定錯誤訊息回報的等級,參數(shù) level 是一個整數(shù)的位元遮罩 (bitmask),見下表。

遮罩值
表示名稱
1
E_ERROR
2
E_WARNING
4
E_PARSE
8
E_NOTICE
16
E_CORE_ERROR
32
E_CORE_WARNING
0 關(guān)閉錯誤報告


  • E_NOTICE 表示一般情形不記錄,只有程式有錯誤情形時才用到,例如企圖存取一個不存在的變數(shù),或是呼叫 stat() 函式檢視不存在的檔案。
  • E_WARNING 通常都會顯示出來,但不會中斷程式的執(zhí)行。這對除錯很有效。例如:用有問題的常規(guī)表示法呼叫 ereg()。
  • E_ERROR 通常會顯示出來,亦會中斷程式執(zhí)行。意即用這個遮罩無法追查到記憶體配置或其它的錯誤。
  • E_PARSE 從語法中剖析錯誤。
  • E_CORE_ERROR 類似 E_ERROR,但不包括 PHP 核心造成的錯誤。
  • E_CORE_WARNING 類似 E_WARNING,但不包括 PHP 核心錯誤警告。


范例

例子 1. error_reporting() examples
<?php

// Turn off all error reporting 關(guān)閉錯誤報告
error_reporting(0);

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

// Report all PHP errors (bitwise 63 may be used in PHP 3)
error_reporting(E_ALL);

// Same as error_reporting(E_ALL);
ini_set(‘error_reporting‘, E_ALL);

?>

set_error_handler
自定義錯誤處理函數(shù)
mixed set_error_handler ( callback error_handler [, int error_types] )
注意:
 ?。?)E_ERROR、E_PARSE、E_CORE_ERROR、E_CORE_WARNING、E_COMPILE_ERROR、E_COMPILE_WARNING是不會被這個句柄處理的,也就是會用最原始的方式顯示出來。不過出現(xiàn)這些錯誤都是編譯或PHP內(nèi)核出錯,在通常情況下不會發(fā)生。

  (2)使用set_error_handler()后,error_reporting ()將會失效。也就是所有的錯誤(除上述的錯誤)都會交給自定義的函數(shù)處理。
其它有關(guān)于set_error_handler()的信息,大家可以參考PHP的官方手冊。

參數(shù):
error_handler 包括4個變量
errno

The first parameter, errno, contains the level of the error raised, as an integer. 錯誤代碼(或E_ERROR等常量)

errstr

The second parameter, errstr, contains the error message, as a string. 錯誤信息

errfile

The third parameter is optional, errfile, which contains the filename that the error was raised in, as a string. 產(chǎn)生錯誤的文件

errline

The fourth parameter is optional, errline, which contains the line number the error was raised at, as an integer. 產(chǎn)生錯誤的行號

errcontext

The fifth parameter is optional, errcontext, which is an array that points to the active symbol table at the point the error occurred. In other words, errcontext will contain an array of every variable that existed in the scope the error was triggered in. 可選參數(shù)


error_types 錯誤類型
Can be used to mask the triggering of the error_handler function just like the error_reporting ini setting controls which errors are shown. Without this mask set the error_handler will be called for every error regardless to the setting of the error_reporting setting

  可用于定義對特定的錯誤類型使用,就像用error_reporting定義一樣。如果缺省,那么每個錯誤(除了E_ERROR、E_PARSE、E_CORE_ERROR、E_CORE_WARNING、E_COMPILE_ERROR、E_COMPILE_WARNING之外的)都會調(diào)用這個函數(shù)。
 
  補充:
Given this code:

class CallbackClass {
   function CallbackFunction() {
       // refers to $this
   }

   function StaticFunction() {
       // doesn‘t refer to $this
   }
}

function NonClassFunction() {
}

there appear to be 3 ways to set a callback function in PHP (using set_error_handler() as an example):

1: set_error_handler(‘NonClassFunction‘);

2: set_error_handler(array(‘CallbackClass‘, ‘StaticFunction‘));

3: $o =& new CallbackClass();
   set_error_handler(array($o, ‘CallbackFunction‘));

The following may also prove useful:

class CallbackClass {
   function CallbackClass() {
       set_error_handler(array(&$this, ‘CallbackFunction‘)); // the & is important
   }
   
   function CallbackFunction() {
       // refers to $this
   }
}

The documentation is not clear in outlining these three examples.

范例

例子 1. Error handling with set_error_handler() and trigger_error()

The example below shows the handling of internal exceptions by triggering errors and handling them with a user defined function:

<?php
// set the error reporting level for this script
error_reporting(E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE);

// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
  switch (
$errno) {
  case
E_USER_ERROR:
    echo
"<b>My ERROR</b> [$errno] $errstr<br />\n";
    echo
"  Fatal error in line $errline of file $errfile";
    echo
", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
    echo
"Aborting...<br />\n";
    exit(
1);
    break;
  case
E_USER_WARNING:
    echo
"<b>My WARNING</b> [$errno] $errstr<br />\n";
    break;
  case
E_USER_NOTICE:
    echo
"<b>My NOTICE</b> [$errno] $errstr<br />\n";
    break;
  default:
    echo
"Unkown error type: [$errno] $errstr<br />\n";
    break;
  }
}

// function to test the error handling
function scale_by_log($vect, $scale)
{
  if (!
is_numeric($scale) || $scale <= 0) {
    
trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
  }

  if (!
is_array($vect)) {
    
trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
    return
null;
  }

  for (
$i=0; $i<count($vect); $i++) {
    if (!
is_numeric($vect[$i]))
      
trigger_error("Value at position $i is not a number, using 0 (zero)", E_USER_NOTICE);
      
$temp[$i] = log($scale) * $vect[$i];
    }
    return
$temp;
  }

// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");

// trigger some errors, first define a mixed array with a non-numeric item
echo "vector a\n";
$a = array(2,3, "foo", 5.5, 43.3, 21.11);
print_r($a);

// now generate second array, generating a warning
echo "----\nvector b - a warning (b = log(PI) * a)\n";
$b = scale_by_log($a, M_PI);
print_r($b);

// this is trouble, we pass a string instead of an array
echo "----\nvector c - an error\n";
$c = scale_by_log("not array", 2.3);
var_dump($c);

// this is a critical error, log of zero or negative number is undefined
echo "----\nvector d - fatal error\n";
$d = scale_by_log($a, -2.5);

?>

上例的輸出類似于:

vector a
                        Array
                        (
                        [0] => 2
                        [1] => 3
                        [2] => foo
                        [3] => 5.5
                        [4] => 43.3
                        [5] => 21.11
                        )
                        ----
                        vector b - a warning (b = log(PI) * a)
                        <b>WARNING</b> [1024] Value at position 2 is not a number, using 0 (zero)<br />
                        Array
                        (
                        [0] => 2.2894597716988
                        [1] => 3.4341896575482
                        [2] => 0
                        [3] => 6.2960143721717
                        [4] => 49.566804057279
                        [5] => 24.165247890281
                        )
                        ----
                        vector c - an error
                        <b>ERROR</b> [512] Incorrect input vector, array of values expected<br />
                        NULL
                        ----
                        vector d - fatal error
                        <b>FATAL</b> [256] log(x) for x <= 0 is undefined, you used: scale = -2.5<br />
                        Fatal error in line 36 of file trigger_error.php, PHP 4.0.2 (Linux)<br />
                        Aborting...<br />

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多