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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度