PHP - Stop displaying full path in errors

只谈情不闲聊 提交于 2020-01-04 04:46:04

问题


Is there any way I can tell PHP to NOT display full path of file having any error in Error, warning or notice messages. I know I can disable errors; But, just to avoid any risk.

For example: My script returns an error which is displayed like:

Fatal error: Call to undefined function shell_exec1() in /home/[user]/public_html/index.php on line 1

I want to display it like

Fatal error: Call to undefined function shell_exec1() in ~/index.php on line 1

This way, It'll be safer way to display error messages while not exposing full path of file to bad guys.

Is this possible? How?


回答1:


Just write your own error handler. see: http://www.php.net/manual/en/function.set-error-handler.php

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    if (!(error_reporting() & $errno)) {
        // This error code is not included in error_reporting
        return;
    }

    switch ($errno) {
        case E_USER_ERROR:
        echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
        echo "  Fatal error on line $errline in file $errfile";
        echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
        echo "Aborting...<br />\n";
        exit(1);
        break;

    case E_USER_WARNING:
        echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
        break;

    case E_USER_NOTICE:
        echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
        break;

    default:
        echo "Unknown error type: [$errno] $errstr<br />\n";
        break;
    }

    /* Don't execute PHP internal error handler */
    return true;
}

set_error_handler("myErrorHandler");



回答2:


As suggested above use custom error handler. BUT cut the path with the basename() to achieve what you want.

http://php.net/manual/ru/function.basename.php



来源:https://stackoverflow.com/questions/21636337/php-stop-displaying-full-path-in-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!