简体   繁体   中英

display_errors = off in php.ini still renders errors to display - and how to log errors?

When I add "display_errors = off" to my php.ini file, I still get some php errors displayed.

BEFORE adding display_errors = off to my php.ini file I was getting full paths, etc displayed (a lot of info. not intended to be seen by a user). AFTER putting display_errors = off in my php.ini file I get much less error info. displayed, however I still get the following error: "Sorry, there was an error: Access denied for user 'username'@'localhost' to database 'mydatabase'. The "Sorry, there was an error" is my custom connect_error die text, and that is all I would like to be displayed. But rendering the username and database I'd like to avoid. Is there something else I need to add to my php.ini file, or is this maybe a function of using connect_error and die in my php code? My LAMP website host does not allow me access to .htaccess and some other config. files. But I do have my own php.ini file I can edit.

Once I get display_error working the way I want I'd like to add log_errors & error_log to my php.ini file so I can log php errors for debugging.

Here is my line in php.ini:

display_errors = off

Here is my check connection code in my php files (in case it helps):

if ($con->connect_error) {
die("Sorry, there was an error: " . $con->connect_error);
}

I expected display_errors = off in my php.ini to give me a blank screen during execution, or only to display "Sorry, there was an error" which would be my preference. Any comments suggestions appreciated. Thank you.

UPDATE: After reading the comments I received to my original post I have attempted to modify some code and add some code.

I added the following line of code just before establishing the mysql db connection so that mysql errors get reported in my php errors log:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

I then added the following lines to my php.ini so that all errors do not display but get written to a php error log:

error_reporting = E_ALL
display_errors = off
log_errors = on
error_log = ../logs/my_php_error_log

I completely commented out my "$con->connect_error" code (below) since all mysql/php errors now get logged to a file anyway, which I check often. NOTE: If NOT checking the connection is not good practice, please let me know:

/*
if ($con->connect_error) {
die("Sorry, there was an error.");
}
*/

All the above changes I made based on what I learned from comments to my original post, and to the more in-depth information I read at the links provided by those that commented. Thank you.

If you do not want the username and database to be rendered, you simply have to remove the $con->connect_error from your custom message.

if ($con->connect_error) {
    die("Sorry, there was an error.");
}

The logic of your code dictates that it should display the error message irregardless of the ini settings. It is never a good idea to die in your code with an error message. PHP has very good error reporting and logic features and you do not need to die or display them yourself.

First of all if you use MySQLi then you should enable exception mode in your code :

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Do not catch your errors and do not display them yourself. If the configuration is switched to display_errors=On and the error reporting is switched on too, you will see the errors in the browser.

If you really want to display your own error messages, you should use exceptions .

if ($con->connect_error) {
    throw new \Exception("Sorry, there was an error: " . $con->connect_error);
}

You could also use trigger_error to trigger PHP error manually.

if ($con->connect_error) {
    throw new \Exception("Sorry, there was an error: " . $con->connect_error, E_USER_ERROR);
}

The worst option of all is to die . If you must die then you can check if error display is switched on first.

if ($con->connect_error) {
    if(ini_get('display_errors')){
        die("Sorry, there was an error: " . $con->connect_error);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM