使用PHP錯誤處理 (轉)

amyz發表於2007-11-07
使用PHP錯誤處理 (轉)[@more@]

Using Error Handling
使用PHP錯誤處理

作者: Mattias Nilsson
譯者: detrox

An all to common error on the is broken links. You end up with broken links from other sites whenever you rearrange your site. People bookmark some page they like and come back 3 months later only to find a '404 Not Found', giving them no help whatsoever about where to start looking for the page you originally had on your site. Let's solve this, or atleast be friendly to your users and help them get back on track whenever they hit 'a 404'. You can even create a common page to report all errors you might encounter while rendering your pages.

在網上最常見的錯誤就是壞連結了。任何時候你重新安排你的站點時,就會終斷在一個來自其他站點的壞連結。一些人標記了他們喜歡的網頁,而3個月後當他們再回去時看的只有一個"404 Not Found", 無論如何這對於那些想尋找你站點上原來那張網頁的人沒有任何幫助。讓我們來解決他,或者至少去友好地對待你的並且當他們碰見一個"404"時幫助他們返回一個存在的網頁。你甚至可以在給你的網頁潤色時建立一個公共頁來報告所有你可能遇到的錯誤。

PHP together with gives you quite alot of free to create your own error pages, but requires some reconfiguring and a tiny bit of coding. Let's start off with the configuration part.

PHP和Apache給了你很多的自由去建立你自己的錯誤處理頁,但需要一些和一小點程式碼。讓我們先從配置開始吧。

The Apache ErrorDocument directive specifies what document (or URI) Apache should redirect a user to in case of an error. It allows you to specify one re for each and every error code one of your users might run into. Start off by adding a

Apache的ErrorDocument指示符指定一個這樣的文件(或URI),當出現一個錯誤時Apache將一個使用者重定向到它。它允許你去制定一個資源,處理你的使用者可能遇到的任何錯誤程式碼。首先,給你的配置加入一個

ErrorDocument 404 /error.php


directive to your server configuration. This will redirect users that ask for a page that does not exist to the 'error.php' page you will soon write. Don't forget to restart Apache for the changes to take effect.

指示符。當使用者查詢的網頁不存在時,它將會把你的使用者引導到一個你將要書寫的網頁'error.php'去。為了讓更改生效別忘了重起Apache伺服器。

Next, we write up a very simple error.php:

下面,我們詳細描述一個簡單的error.php

The file you requested (=$REDIRECT_URL?>) does not exist on this server. Please look for the page you wanted at the front page


Now try to access a page that doesn't exist on your server, and voila, you're at the error.php page with a nice and friendly message and a link to your front page!

現在試著去讀取一個伺服器上不存在的網頁,然後瞧,你來到了error.php,上面有一個既漂亮又友好的提示資訊和一個到你的首頁的連線

Let's extend this. As you can see, I used the REDIRECT_URL variable on the error.php page. This is a variable that Apache sets whenever it invokes an ErrorDocument directive, and gives you a possibility to find the originating resource whenever there's an error. Apache also sets a number of other variables in this case, all of them documented here. Use theese variables to create a nice error page that gives your users a nice and friendly error page instead of the good ol' boring Apache default page.

讓我再來擴充套件它。正像你所看到的,我在error.php裡使用了REDIRECT_URL變數。當Apache一個ErrorDocument指示符時這個變數被設定,並且不論何時出現錯誤,你都有可能找到引發它的資源。對於這種情況,Apache同樣設定了一系列的其他變數,他們都在這裡被提到。使用這些變數去建立一個完美的錯誤處理頁,來給你的使用者一個漂亮且友好的錯誤處理,以此取代那些無聊的Apache預設頁。

Throwing errors from a PHP page is quite the same as emulating Apache's behaviour for ErrorDocument directives, you simply redirect the user using a query-string that specifies variables that Apache usually sets as environment variables. This makes it possible to use the same error page for all kinds of errors. An example:

在PHP頁中丟擲錯誤可以完全效仿Apache的ErrorDocument指示符的行為,你可以使用一個制定了變數的查詢字串簡單地重定向使用者,這些變數通常是Apache作為環境變數設定的。這使得使用一個錯誤處理頁面處理所有的錯誤成為可能,例如:

function throw_error($message) {
  $error_page = "/err/error.php";
  $error_url = $error_page;
  $error_url .= "?REDIRECT_ERROR_NOTES=$message";
  $error_url .= "&REDIRECT_URL=" . $GLOBALS["PHP_SELF"];
  $error_url .= "&REDIRECT_REQUEST_METHOD=$REQUEST_METHOD";
  $error_url .= "&REDIRECT_STATUS=501";
  Header("Status: 501");
  Header("Location: $error_url");
  exit;
}

ob_start(); // Use output buffering to be able to throw errors anywhere on this page.

if(!condition) {
  throw_error("the condition failed");
}

ob_end_flush(); // Page rendered, flush the output buffer.
?>


sing the feature called output buffering also helps creating generic error reporting functionality. By not flushing the output buffer until you are sure the whole page has rendered error-free, you are able to make Header calls anywhere in your code to redirect the user.

看那個PHP4的叫做輸出緩衝的特性也給我們建立一般性錯誤報告帶來了幫助。因為直到你確定整張網頁被處理的沒有一點錯誤時才輸出這個緩衝區的內容,你可以在程式碼的任何地方使用Header呼叫去重定向使用者。

I'll leave up to the reader to design and implement his/her own error.php page to suit his/her site. Don't forget that you can include a fowith e submission possibilities for the users to send you comments..

我將留給讀者自己去設計和實現他們自己的合適自己站點的error.php頁。別忘了,你可以包含一個帶有傳送的表單,這使得你的使用者可 以給你提供意見.


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-981161/,如需轉載,請註明出處,否則將追究法律責任。

相關文章