dongxiaoyan4388 2011-02-17 19:12
浏览 70
已采纳

PHP自定义错误页面

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?

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?

  • 写回答

2条回答 默认 最新

  • dth54864 2011-02-17 21:51
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图