問題描述
每個人都說在活動站點中允許顯示錯誤"是不好的(由于一些安全問題).
Everyone says that "Enabling errors to be shown" in an active site is bad (due to some security issues).
現在,我們必須考慮兩種情況:
Now, we have to consider 2 cases:
- 網站處于調試模式
- 該站點未處于調試模式
現在,對于案例 #1:
我們想查看錯誤.怎么樣?
We want to see the errors. How?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
沒有比這更簡單的了.我們還可以為除 Parse 和 Fatal 之外的所有錯誤自定義錯誤處理程序.
Nothing more simple. Also we can customize an error handler for all errors except Parse and Fatal.
相反,如果情況是 #2:
我們希望能夠停用這些消息:
We would like to be able to deactivate the messages:
ini_set('error_reporting', 0);
ini_set('display_errors', 0);
沒關系.但是如何向用戶顯示一條友好的消息,例如嘿,伙計,有些事情真的很糟糕.我不向您保證我們正在努力解決它,因為我們很懶惰.".您應該再次啟用錯誤并只使用函數 set_error_handler()
并希望不會發生解析或致命錯誤.但我的第一個問題是:
And it's ok. But what about showing users a friendly message such as "Hei man, something is really f**ked up. I don't assure you we are working to fix it, since we are very lazy.".
You should enable errors again and just use the function set_error_handler()
and hope that no parse or fatal errors occur. But my first question is:
問題 1:是否可以避免錯誤報告并在出現問題時加載自定義離線頁面?我的意思是,是否有可能有 ini_set('error_reporting', 0);
和 ini_set('display_errors', 0);
并且仍然能夠告訴 PHP 加載自定義錯誤頁面?
Question 1: Is that possible to avoid error reporting and have a custom offline page that is loaded when something goes wrong? I mean, is it possible to have
ini_set('error_reporting', 0);
andini_set('display_errors', 0);
and still be able to tell PHP to load a custom Error page?
現在是另一個:
問題 2:我開發了一個類,利用 set_error_handler()
的強大功能將發生的錯誤記錄到數據庫中.通過這種方式,我可以跟蹤黑客嘗試和其他很酷的東西.(是的,我總是確定數據庫是可訪問的,因為如果我們無法連接到數據庫,我的應用程序就會關閉).這值得嗎?
Question 2: I developed a class that with the power of
set_error_handler()
logs errors occurred into the database. In this way I can keep track of hack attempts and other cool stuff. (And yes, i'm always sure the DB is accessible since my application shuts down if we cannot connect to the DB). Is this worth something?
推薦答案
前段時間我創建了一個小系統,當發生致命錯誤/拋出未捕獲的異常時,它會將您重定向到錯誤頁面.有可能假設每個請求都由一個文件處理并以該文件結尾,因此通過到達該文件的末尾,我確定一切正常.在這種情況下,我設置了在錯誤頁面上重定向的函數并將其注冊為關閉函數 - 因此它將在所有請求結束時調用.現在在這個函數中,我檢查干凈關閉的條件,如果滿足,我什么都不做,輸出被刷新到瀏覽器,否則緩沖區被清除,只發送重定向到錯誤頁面的標頭.
Some time ago I created small system that redirects you to error page when fatal error occurs / uncaught exception was thrown. It was possible with assumption, that every request is handled by one file and ends in this file, so by reaching end of this file I'm sure that everything went OK. With this condition I've set up function to redirect on error page and registered it as shutdown function - so it will be called at the end of all requests. Now in this function I check conditions for clean shutdown and if hey are met, I do nothing and output is flushed to the browser, otherwise buffer is cleaned and only header redirecting to error page is sent.
此代碼的簡化版本:
<?php
function redirect_on_error(){
if(!defined('EVERYTHING_WENT_OK')){
ob_end_clean();
header('Location: error.html');
}
}
register_shutdown_function('redirect_on_error');
ob_start();
include 'some/working/code.php';
echo "Now I'm going to call undefined function or throw something bad";
undefined_function();
throw new Exception('In case undefined function is defined.');
define('EVERYTHING_WENT_OK', TRUE);
exit;
這篇關于PHP 自定義錯誤頁面的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!