王朝网络
分享
 
 
 

使用PHP错误处理

王朝php·作者佚名  2006-01-08
宽屏版  字体: |||超大  

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发送的表单,这使得你的用户可 以给你提供意见.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有