PHP - 停止显示错误的完整路径 [英] PHP - Stop displaying full path in errors

查看:80
本文介绍了PHP - 停止显示错误的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法可以让PHP不能在错误,警告或通知消息中显示文件的完整路径。我知道我可以禁用错误;但是,只是为了避免任何风险。



例如:我的脚本返回错误,显示如下:



致命错误:调用未定义的函数shell_exec1()在/home/[user]/public_html/index.php第1行



我想显示它像



致命错误:在第1行〜/ index.php中调用未定义的函数shell_exec1()



这样,在没有将完整路径的文件暴露给坏家伙的情况下,显示错误消息将更安全。


$ b $这是可能吗?如何?

解决方案

只需编写自己的错误处理程序。请参阅: http://www.php.net/manual/en /function.set-error-handler.php

  function myErrorHandler($ errno,$ errstr,$ errfile, $ errline){
if(!(error_reporting()& $ errno)){
//此错误代码不包含在error_reporting
return;
}

开关($ errno){
case E_USER_ERROR:
echo< b>我的错误< / b> [$ errno] $ errstr /> \\\
;
echo文件$ errfile中的$ errline上的致命错误;
echo,PHP。 PHP_VERSION。 (。PHP_OS)< br /> \\\
;
echoAborting ...< br /> \\\
;
exit(1);
break;

case E_USER_WARNING:
echo< b>我的警告< / b> [$ errno] $ errstr< br /> \\\
;
break;

case E_USER_NOTICE:
echo< b>我的NOTICE< / b> [$ errno] $ errstr< br /> \\\
;
break;

默认值:
echo未知错误类型:[$ errno] $ errstr< br /> \\\
;
break;
}

/ *不执行PHP内部错误处理程序* /
return true;
}

set_error_handler(myErrorHandler);


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?

解决方案

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");

这篇关于PHP - 停止显示错误的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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