PHP - Stop displaying full path in errors

2019-07-06 12:59发布

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?

2条回答
Bombasti
2楼-- · 2019-07-06 13:20

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

查看更多
Luminary・发光体
3楼-- · 2019-07-06 13:37

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");
查看更多
登录 后发表回答