how to avoid strace in php

我的未来我决定 提交于 2019-12-13 03:41:34

问题


I'm having PHP + MySQL code base. If MySQL is stopped and during log-in it fails like this:

Fatal error: Uncaught exception 'FrameworkException' with message 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)' in /var/www/html/classes/class.connection.php:24 

Stack trace: #0 /var/www/html/login.php(15): Connection::getInstance('localhost', 'user', '__password', 'DB') #1 /var/www/html/login.php(73): isDataValid('lakshmipathi.g@...', 'test') #2 {main} thrown in /var/www/html/classes/class.connection.php on line 24 

How to avoid this stack trace? I want to avoid the username and password displayed like this in case of errors


回答1:


On a production server you should hide the error with the display_errors directive of your php.ini The error still be logged but not displayed to the end user. Turning off the error with error_reporting is not a good idea if you need to debug

Nevertheless you should handle the exception :

try {
//Here the code which can throw an exception
} catch(FrameworkException e) {
 echo 'an exception occured';
}



回答2:


I'd recommend using @mysql_connect (or @ + whatever function you call) to suppress any error messages. And manually spit out some error. Since you are using some kind of mysterious framework, you could simply catch the exception.

But, for live environments, you should disable any kind error reporting to the user anyway.



来源:https://stackoverflow.com/questions/5262245/how-to-avoid-strace-in-php

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