天天看點

php環境代碼錯誤定位,PHP 如何定位 500 錯誤

顯示行号

|

選擇喜歡的代碼風格

預設

GitHub

Dune

LakeSide

Plateau

Vibrant

Blue

Eighties

Tranquil

在寫伺服器端代碼的時候,有時候經常會遭遇到500錯誤,本節将介紹PHP開發中定位500錯誤。

在開發PHP之前,我感覺有必要跟大家介紹下,捕獲500錯誤的一些方法,因為經常遇到。有幾種方式,首先需要檢視伺服器錯誤日志,在代碼中一些設定(最好在開發環境中設定,而生産環境中隐藏這些敏感的報錯資訊):

檢視伺服器錯誤日志,{php dir}/var/log/php_errors.log

設定 error_reporting(E_ALL)

設定 ini_set("display_errors", "On");

安裝 Xdebug,調試錯誤資訊

進階進階,利用 Linux 的 strace 指令

在PHP代碼中加入如下萬能函數,捕捉顯示服務端錯誤資訊(注意,不要在生産環境中這麼做)

register_shutdown_function(function(){ var_dump(error_get_last()); });

附:檢視 PHP 錯誤日志配置

[[email protected]]$php -i|grep error

display_errors => Off => Off

display_startup_errors => Off => Off

error_append_string => no value => no value

error_log => /Data/apps/php-fcgi/var/log/php_errors.log =>

/Data/apps/php-fcgi/var/log/php_errors.log

error_prepend_string => no value => no value

error_reporting => 4177 => 4177

html_errors => Off => Off

ignore_repeated_errors => Off => Off

log_errors => On => On

log_errors_max_len => 1024 => 1024

track_errors => Off => Off

xmlrpc_error_number => 0 => 0

xmlrpc_errors => Off => Off

PHP 捕捉錯誤擴充閱讀:

發表評論