Вопрос

My PHP version is 5.5.7.

I set error_reporting(E_ALL);

then I intentionally wrote my database password wrong.

when I was using die() before usage of error_log(), my screen displays 2 messages: A warning and error:

Warning: mysqli::mysqli(): (42000/1044): Access denied for user 'user'@'%' to database 'database' in /folder/folder/folder/www/folder/file.php on line 2

Connect Error (1044) Access denied for user 'user'@'%' to database 'database'

And after deleting die() codes and applying the code below, I expected all logs on the custom log file. However; while the Log file has the message I created, I still got the warning on screen with sensitive info.

$DBconn = new mysqli($DBhost, $DBuser, $DBpass, $DBname, $DBport);

if ($DBconn->connect_error) 
{           
    error_log("Connect Error! ".$DBconn->connect_errno.' '.$DBconn->connect_error."\n\r", 3, "/home/www/host/PHP_errors.log");
}

question:

what am I doing wrong? how can I achieve all kind of errors are printed into log file?

Это было полезно?

Решение

By default, mysqli throws PHP warnings for errors. This is not ideal, especially in your situation where you want to see errors but do not want the sensitive information about your database displayed on the screen.

What I recommend is that you change the report mode of mysqli to throw Exceptions instead - this way you can catch them and then handle and log. My example below shows how you might do that:

mysqli_report(MYSQLI_REPORT_STRICT);

try {

    $DBconn = new mysqli($DBhost, $DBuser, $DBpass, $DBname, $DBport);

} catch (Exception $e) {

    error_log(
        "Connect Error! ".$e->getMessage()."\n",
        3,
        "/home/www/host/PHP_errors.log"
    );

}

If you're not a fan of exceptions - then you can disable reporting of PHP warnings by changing your error_reporting declaration to exclude them like this:

error_reporting(E_ALL ^ E_WARNING);

But this will disable reporting of all warnings - not just those from mysqli.

Другие советы

The display of errors on screen is controlled by the display_errors directive. Setting the other two is independent of that.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top