1

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.

obcbeatle
  • 81
  • 1
  • 6
  • Part of your program's logic is to display an error. Even if they are switched off on PHP level your piece of code displays them. [That is why it is never a good idea to die with an error message.](https://stackoverflow.com/a/15320411/1839439) – Dharman Jun 30 '19 at 20:21
  • die() is not an error, it's just an output like echo. Use Exceptions or `trigger_error()` https://www.php.net/manual/en/function.trigger-error.php – lufc Jun 30 '19 at 21:13
  • Thank you both for clarifying my program logic mistake. And for clarifying what die() means, plus the links. – obcbeatle Jul 02 '19 at 22:08

2 Answers2

1

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.");
}
Chin Leung
  • 14,621
  • 3
  • 34
  • 58
-1

The logic of your code dictates that it should display the error message regardless 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);
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Thank you for the in-depth information and links. I have made some modifications and will attempt to edit my original question to include some changes I made. – obcbeatle Jul 02 '19 at 22:16
  • @obcbeatle Your edit looks good. Well done. Does it mean that your question is answered or do you still have some issues with error logging? – Dharman Jul 02 '19 at 23:08
  • I think that's all the questions I have, for now :-) Although I probably need to come up with a script or something to keep an eye on the php error log so that it doesn't run away on me and get out of hand (file size). But right now that error log primary has errors from when I'm debugging prepared statements, which I'm learning how to write with different queries, etc.. But right now everything seems to be working as I think it should :-) Thank you for the help! – obcbeatle Jul 03 '19 at 03:32
  • You should accept one of the answers to let others know you solved your problem. – Dharman Jul 03 '19 at 07:57