15

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);.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
andrew
  • 5,096
  • 10
  • 43
  • 57

4 Answers4

32

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');
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • I like the idea of turning on errors when the admin is logged on, then whenever i check the site i can immediately see what is wrong without having to turn on errors. Thanks – andrew Dec 01 '09 at 08:39
  • You're welcome (I sometimes uses that trick too -- it's an easy way, much easier than going through log files, anyway) :-) Have fun ! – Pascal MARTIN Dec 01 '09 at 12:12
  • 2
    I've added this answer as the canonical answer to "How do I turn on error reporting?" into the PHP tag wiki. – Pekka May 01 '11 at 12:37
4

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!

NDM
  • 6,731
  • 3
  • 39
  • 52
1

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, ...

Brimstedt
  • 3,020
  • 22
  • 32
  • That sounds interesting, sending them to my email would be particularly useful, ill do some reading on that thanks – andrew Dec 01 '09 at 08:37
1

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.

Alex Weinstein
  • 9,823
  • 9
  • 42
  • 59