php - 当display_errors=0并且页面有错误时如何防止页面损坏?
晚上好,
当 display_errors=0 并且页面有错误并且我不知道什么“条件、代码等...”会导致这些错误时,如何设置自定义错误或防止页面损坏?
例如,在这段代码中:
<?php
/////////////////////////////////////////////
ini_set("display_errors" , "0");
/////////////////////////////////////////////
$html = '
<table>
<tr>
<td><img src="img0.gif" /></td>
<td><img src="img1.gif" /></td>
<td><img src="img2.gif" /></td>
</tr>
</table>
';
/////////////////////////////////////////////
$dom = new domDocument();
$dom -> loadHTML($html);
$xpath = new domXPath($dom);
/////////////////////////////////////////////
$query = $xpath->query('.//table/tr/td');
$image = $query->item(1000000000); // <<---- item number
$img_src = $image->getElementsByTagName('img')->item(0)->getAttribute('src');
echo $img_src;
/////////////////////////////////////////////
?>
在该代码中因为没有 item(1000000000)
,所以会导致错误..
并且因为 display_errors" , "0"
所有错误都将被隐藏并且页面被损坏
那么,如何防止这种损害?
我知道我可以通过添加简单的条件来防止它,例如:
if( $image <1 ){ die('错误消息'); } $img_src
var
之前 但是如果我有超过 10000 行的大代码并且我不知道到底什么代码会导致错误怎么办!!
我们可以通过 httaccess 来阻止它吗?通过检查页面源是否为空重定向到错误页面..
或者我们可以通过 php 本身的一些函数来阻止它吗?
有什么想法吗?
good evening,
how to set custom error OR prevent page damage when display_errors=0 AND the page have errors AND i do not know what 'condition, code, etc...' will cause these errors ?
for example, in this code :
<?php
/////////////////////////////////////////////
ini_set("display_errors" , "0");
/////////////////////////////////////////////
$html = '
<table>
<tr>
<td><img src="img0.gif" /></td>
<td><img src="img1.gif" /></td>
<td><img src="img2.gif" /></td>
</tr>
</table>
';
/////////////////////////////////////////////
$dom = new domDocument();
$dom -> loadHTML($html);
$xpath = new domXPath($dom);
/////////////////////////////////////////////
$query = $xpath->query('.//table/tr/td');
$image = $query->item(1000000000); // <<---- item number
$img_src = $image->getElementsByTagName('img')->item(0)->getAttribute('src');
echo $img_src;
/////////////////////////////////////////////
?>
in that code because there is no item(1000000000)
, so it will cause error ..
and bacause display_errors" , "0"
all errors will be hidden and the page be damaged
So, how to prevent that damage ?
i know i can prevent it by adding simple condition like :if( $image <1 ){ die('error msg'); }
before $img_src
var
but what about if i have a big code more thane 10000 line and i do not know exactly what code will cause error !!
can we prevent it by httaccess ? by checking page source if it empty redirect to error page ..
or can we prevent it by some functions in php itself ?
any ideas ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 set_error_handler 。要设置错误情况下的回调,您可以在其中终止脚本进一步执行。
更新
刚刚发现自定义错误处理程序不适用于 E_ERROR 和其他错误类型。这是 PHP 文档中的内容
所以这是我在互联网上找到的一种解决方法,我已经用您的脚本对其进行了测试并且工作正常。
将以下代码添加到脚本的开头:
这将注册一个关闭函数,该函数在脚本因致命错误而终止时执行,无论 display_errors 是打开还是关闭。
我们检查最后一个错误是否是致命错误,如果是,我们重定向到 error_page.php。
注意:需要注意的一件事是,如果 display_errors 为 ON,则重定向仍然有效。我已经测试过它并且工作正常。这可能是因为我的服务器上启用了output_buffering。
更新2:
这是完整的 PHP 文件,其中添加了您的代码和关闭处理程序。这工作得很好并重定向到 error_page.php。将其更改为您要重定向到的页面:
You can use set_error_handler . To set a callback ion case of error and in there you can terminate the script from executing further.
Update
Just found out custom error handler does not work with E_ERROR and other error types. This is in PHP Documentation
So this is a workaround that I found on internet and I have tested it with your script and is working fine.
Add the following code to the start of your script:
This registers a shutdown function which is executed on script termination from a fatal error, irrespective of whether display_errors is on or off.
And there we check if last error was a fatal one, if so we redirect to error_page.php.
Note: One thing to note is that the redirect is STILL working if display_errors is ON. I have tested it and it is working fine. This maybe because of output_buffering being enabled on my server though.
Update 2:
This is the complete PHP file with Your code with shutdown handler added. This works just fine and redirects to error_page.php. Change this to the page you want to redirect to.:
如果您想对用户隐藏所有错误,请启用 error_reporting(以便错误仍被记录)并禁用 display_errors。
如果您希望所有错误都是致命的(即停止页面处理),您可以设置一个自定义错误处理程序,该处理程序会在每次错误时终止。
If you want to hide all errors from your users, enable error_reporting (so that the error still gets logged) and disable display_errors.
If you want all errors to be fatal (i.e. stop page processing) you can set a custom error handler which dies on every error.