首页 > 应用开发 > IT综合 > 使用PHP错误处理 (转)

使用PHP错误处理 (转)

原创 IT综合 作者:amyz 时间:2007-11-07 17:36:35 0 删除 编辑
使用PHP错误处理 (转)[@more@]

Using PHP Error Handling

作者: Mattias Nilsson
译者: detrox

An all to common error on the web 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 apache gives you quite alot of freedom to create your own error pages, but requires some reconfiguring and a tiny bit of coding. Let's start off with the configuration part.


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 resource for each and every error code one of your users might run into. Start off by adding a


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.


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


The file you requested () 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!


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.


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:


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_STATUS=501";
  Header("Status: 501");
  Header("Location: $error_url");

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 php4 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.


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 foRM with eMail submission possibilities for the users to send you comments..

我将留给读者自己去设计和实现他们自己的合适自己站点的error.php页。别忘了,你可以包含一个带有email发送的表单,这使得你的用户可 以给你提供意见.

来自 “ ITPUB博客 ” ,链接:,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
  • 博文量
  • 访问量