PHP错误页面与异常显示错误页面的设计影响用户体验。开发环境显示详细错误信息生产环境显示友好的错误页面。今天说说PHP错误页面的处理。开发环境的错误显示。phpini_set(display_errors, 1);ini_set(error_reporting, E_ALL);?生产环境的错误处理。phpini_set(display_errors, 0);ini_set(log_errors, 1);ini_set(error_log, /var/log/php_errors.log);set_exception_handler(function (Throwable $e) {error_log(未捕获: {$e-getMessage()} in {$e-getFile()}:{$e-getLine()});http_response_code(500);header(Content-Type: application/json; charsetutf-8);$response [error true,message 服务器内部错误,];if ($_SERVER[APP_ENV] development) {$response[message] $e-getMessage();$response[file] $e-getFile();$response[line] $e-getLine();}echo json_encode($response, JSON_UNESCAPED_UNICODE);});set_error_handler(function (int $level, string $message, string $file, int $line) {throw new ErrorException($message, 0, $level, $file, $line);});?自定义错误页面。phpclass ErrorPage{public static function show(int $code, string $message ): void{http_response_code($code);$title self::getTitle($code);$message $message ?: self::getDefaultMessage($code);echo {$title}{$code}{$title}{$message};exit;}private static function getTitle(int $code): string{return match ($code) {400 错误请求,401 未授权,403 禁止访问,404 页面不存在,405 方法不允许,429 请求过多,500 服务器错误,502 网关错误,503 服务不可用,default 错误,};}private static function getDefaultMessage(int $code): string{return match ($code) {404 您访问的页面不存在,500 服务器遇到错误请稍后重试,503 服务正在维护请稍后访问,default 请求处理出错,};}}// ErrorPage::show(404);?保存错误信息到日志。phpclass ErrorLogger{private string $logDir;private string $env;public function __construct(string $logDir /var/log/app, string $env production){$this-logDir rtrim($logDir, /);$this-env $env;if (!is_dir($this-logDir)) mkdir($this-logDir, 0755, true);}public function log(Throwable $e, array $context []): void{$entry [time date(c),env $this-env,type get_class($e),message $e-getMessage(),file $e-getFile(),line $e-getLine(),trace $e-getTraceAsString(),context $context,];file_put_contents($this-logDir . /errors- . date(Y-m-d) . .log,json_encode($entry, JSON_UNESCAPED_UNICODE) . \n,FILE_APPEND | LOCK_EX);}}?好的错误处理提升用户体验。开发环境显示详细错误帮助调试生产环境隐藏错误细节保护安全。统一的错误页面让用户看到友好的提示而不是难看的错误堆栈。
PHP错误页面与异常显示
PHP错误页面与异常显示错误页面的设计影响用户体验。开发环境显示详细错误信息生产环境显示友好的错误页面。今天说说PHP错误页面的处理。开发环境的错误显示。phpini_set(display_errors, 1);ini_set(error_reporting, E_ALL);?生产环境的错误处理。phpini_set(display_errors, 0);ini_set(log_errors, 1);ini_set(error_log, /var/log/php_errors.log);set_exception_handler(function (Throwable $e) {error_log(未捕获: {$e-getMessage()} in {$e-getFile()}:{$e-getLine()});http_response_code(500);header(Content-Type: application/json; charsetutf-8);$response [error true,message 服务器内部错误,];if ($_SERVER[APP_ENV] development) {$response[message] $e-getMessage();$response[file] $e-getFile();$response[line] $e-getLine();}echo json_encode($response, JSON_UNESCAPED_UNICODE);});set_error_handler(function (int $level, string $message, string $file, int $line) {throw new ErrorException($message, 0, $level, $file, $line);});?自定义错误页面。phpclass ErrorPage{public static function show(int $code, string $message ): void{http_response_code($code);$title self::getTitle($code);$message $message ?: self::getDefaultMessage($code);echo {$title}{$code}{$title}{$message};exit;}private static function getTitle(int $code): string{return match ($code) {400 错误请求,401 未授权,403 禁止访问,404 页面不存在,405 方法不允许,429 请求过多,500 服务器错误,502 网关错误,503 服务不可用,default 错误,};}private static function getDefaultMessage(int $code): string{return match ($code) {404 您访问的页面不存在,500 服务器遇到错误请稍后重试,503 服务正在维护请稍后访问,default 请求处理出错,};}}// ErrorPage::show(404);?保存错误信息到日志。phpclass ErrorLogger{private string $logDir;private string $env;public function __construct(string $logDir /var/log/app, string $env production){$this-logDir rtrim($logDir, /);$this-env $env;if (!is_dir($this-logDir)) mkdir($this-logDir, 0755, true);}public function log(Throwable $e, array $context []): void{$entry [time date(c),env $this-env,type get_class($e),message $e-getMessage(),file $e-getFile(),line $e-getLine(),trace $e-getTraceAsString(),context $context,];file_put_contents($this-logDir . /errors- . date(Y-m-d) . .log,json_encode($entry, JSON_UNESCAPED_UNICODE) . \n,FILE_APPEND | LOCK_EX);}}?好的错误处理提升用户体验。开发环境显示详细错误帮助调试生产环境隐藏错误细节保护安全。统一的错误页面让用户看到友好的提示而不是难看的错误堆栈。