使用PHP错误处理

类别:编程语言 点击:0 评论:0 推荐:
Using PHP Error Handling
使用PHP错误处理

作者: 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.

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 resource 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 <A HREF="/">the front page</A>

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作为环境变量设置的。这使得使用一个错误处理页面处理所有的错误成为可能,例如:

<?php
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 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.

看那个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 form with email submission possibilities for the users to send you comments..

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

本文地址:http://com.8s8s.com/it/it29215.htm