Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP production server - turn on error messages

Tags:

php

This question has been asked before in a more general way. I want to display error messages on a particular page on my production server, and I do not have access to the php.ini file. What is the best way to enable all errors and warnings on a particular PHP page on your production server?

I have tried ERROR_REPORTING(E_ALL);.

like image 791
andrew Avatar asked Dec 01 '09 06:12

andrew


People also ask

How do I enable PHP error messages?

Quickly Show All PHP Errors The quickest way to display all php errors and warnings is to add these lines to your PHP code file: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

How do I enable PHP error logging?

Enable Error Logging in php. If you want to enable PHP error logging in individual files, add this code at the top of the PHP file. ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); Now, you must enable only one statement to parse the log errors in the php.


4 Answers

To enable errors, you must use error_reporting before the point where those are triggered (for example, at the beginning of your PHP script) :

error_reporting(E_ALL);

And to have the error displayed, you should configure display_errors :

ini_set('display_errors', 'On');

(This one should be disabled on a production server, which means you might have to enable it this way, even after having configured error_reporting)


Of course, all this can be encapsulated in an if block, to make sure only you can see the error messages -- especially if you are doing this on a live production website ; for instance :

if ($_SESSION['is_admin'])
{
    error_reporting(E_ALL);
    ini_set('display_errors', 'On');
}


And to get things a bit prettier, you might also want to configure html_errors :

ini_set('html_errors', 'On');
like image 112
Pascal MARTIN Avatar answered Oct 08 '22 19:10

Pascal MARTIN


you should really not display them on a production server. Best way is to create some logging system.

Keep in mind to make it reusable!

like image 42
NDM Avatar answered Oct 08 '22 20:10

NDM


A different method all together would be to register an error handler with: set_error_handler

This way, you can chose what to do with the errors: Mail them to admin, display friendly error message, log to file/db, ...

like image 24
Brimstedt Avatar answered Oct 08 '22 21:10

Brimstedt


One other thing to note is that you probably not only want to display errors, you want to log them to file/database. Just showing errors to your users in the production environment is not enough; asking them to report the problems will lead to you not knowing what's wrong with your server until it's too late. In your base error and exception handler, make sure to have logic that records the issue (as much debugging info as possible - stack trace, user IP, browser, application version, etc) into persistent storage.

like image 40
Alex Weinstein Avatar answered Oct 08 '22 21:10

Alex Weinstein