PHP自定义错误页面 [英] PHP custom error page

查看:461
本文介绍了PHP自定义错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,好吧。每个人都说,在活动网站中显示允许显示错误是坏的(由于某些安全问题)。



现在,我们必须考虑2种情况: p>


  1. 该网站处于调试模式

  2. 该网站未处于调试模式

现在,对于案例#1:



我们想看到错误。怎么样?
嗯:

  ini_set('error_reporting',E_ALL); 
ini_set('display_errors',1);

没有什么比较简单。另外,我们可以为除Parse和Fatal之外的所有错误自定义一个错误处理程序。



如果是#2:



我们希望能够停用消息:

  ini_set('error_reporting ',0); 
ini_set('display_errors',0);

没关系但是,如何向用户显示一个友好的信息,如黑人,真的是有点麻烦了,我不保证,我们正在努力解决它,因为我们很懒惰。
您应该再次启用错误,只需使用函数 set_error_handler(),并希望不会发生解析或致命错误。但是我的第一个问题是:


问题1 :是否可以避免错误报告,并自定义离线出现错误时加载的页面?我的意思是说,可能有 ini_set('error_reporting',0); ini_set('display_errors',0); 还能够告诉PHP加载一个自定义错误页面?


现在另一个:


问题2 :我开发了一个具有 set_error_handler()数据库中发生日志错误。以这种方式,我可以跟踪黑客尝试和其他酷的东西。 (是的,我总是确定DB可以访问,因为如果我们的应用程序关闭,如果我们无法连接到DB)。这是值得的吗?



解决方案

前段时间我创建了小型系统,将您重定向到错误当发生致命错误/未捕获异常的页面被抛出。这是可能的假设,每个请求由一个文件处理,并结束在这个文件,所以通过到这个文件的结尾,我确信一切顺利。有了这个条件,我已经设置了在错误页面上重定向的功能,并将其注册为关闭功能 - 因此它将在所有请求结束时调用。现在在这个功能我检查条件干净的关机,如果他满足,我什么也不做,输出刷新到浏览器,否则缓冲区被清除,只有标题重定向到错误页面。



此代码的简化版本:

   function redirect_on_error(){
if(!defined('EVERYTHING_WENT_OK')){
ob_end_clean();
header('Location:error.html');
}
}

register_shutdown_function('redirect_on_error');

ob_start();

包含some / working / code.php;

echo现在我打算调用未定义的函数或扔东西不好;

undefined_function();
抛出新的异常('定义未定义的函数');

define('EVERYTHING_WENT_OK',TRUE);
退出;


Ok, so. Everyone says that "Enabling errors to be shown" in an active site is bad (due to some security issues).

Now, we have to consider 2 cases:

  1. The site is in debug mode
  2. The site is not in debug mode

Now, for case #1:

We want to see the errors. How? Well:

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

Nothing more simple. Also we can customize an error handler for all errors except Parse and Fatal.

Instead, if the case is #2:

We would like to be able to deactivate the messages:

ini_set('error_reporting', 0);
ini_set('display_errors', 0);

And it's ok. But what about showing users a friendly message such as "Hei man, something is really f**ked up. I don't assure you we are working to fix it, since we are very lazy.". You should enable errors again and just use the function set_error_handler() and hope that no parse or fatal errors occur. But my first question is:

Question 1: Is that possible to avoid error reporting and have a custom offline page that is loaded when something goes wrong? I mean, is it possible to have ini_set('error_reporting', 0); and ini_set('display_errors', 0); and still be able to tell PHP to load a custom Error page?

And now another:

Question 2: I developed a class that with the power of set_error_handler() logs errors occurred into the database. In this way I can keep track of hack attempts and other cool stuff. (And yes, i'm always sure the DB is accessible since my application shuts down if we cannot connect to the DB). Is this worth something?

解决方案

Some time ago I created small system that redirects you to error page when fatal error occurs / uncaught exception was thrown. It was possible with assumption, that every request is handled by one file and ends in this file, so by reaching end of this file I'm sure that everything went OK. With this condition I've set up function to redirect on error page and registered it as shutdown function - so it will be called at the end of all requests. Now in this function I check conditions for clean shutdown and if hey are met, I do nothing and output is flushed to the browser, otherwise buffer is cleaned and only header redirecting to error page is sent.

Simplified version of this code:

<?php
function redirect_on_error(){
    if(!defined('EVERYTHING_WENT_OK')){
        ob_end_clean();
        header('Location: error.html');
    }
}

register_shutdown_function('redirect_on_error');

ob_start();

include 'some/working/code.php';

echo "Now I'm going to call undefined function or throw something bad";

undefined_function();
throw new Exception('In case undefined function is defined.');    

define('EVERYTHING_WENT_OK', TRUE);
exit;

这篇关于PHP自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆