How to make sure that server errors are shown? - php

I need to test some functionality on live server and it's obvious that they turned off errors at some global level.
Problem is that I can't work when I don't know what's throwing the error. How can I make sure that exceptions are being shown and not just 500 code.
I have tried putting these two lines at the top of my script but it's still empty.
<?php
ini_set('error_reporting', E_ALL);
error_reporting(E_ALL);
throw new Exception('Fatal Error'); // nothing is outputed

The two lines are pretty much identical, so remove one of them.
You also need ini_set("display_errors",1); to actually display errors.
Note however that this will not work for syntax errors in the current file, as these occur in the parsing phase, before any statements are actually run.

Related

PHP error reporting destination: error log file vs. web page

In the following code, I get the 'AAA' on the web page output (only), while the 'BBB' goes to the error.log file (only).
I want both to go to the error.log file, so the user doesn't see potential errors while I do. How?
<?php
ini_set('display_errors','1');
ini_set('error_log','error.log');
error_reporting(-1); // all
trigger_error('AAA');
error_log('BBB');
?>
(Note: This is similar to http://stackoverflow.com/questions/9921643/how-to-make-provoke-an-php-error-in-php-error-log but not the same.)
Set ini_set('display_errors', 0);
OR
Create your own error handler function and add it before calling trigger_error function with set_error_handler function.
This function can be used for defining your own way of handling errors
during runtime, for example in applications in which you need to do
cleanup of data/files when a critical error happens, or when you need
to trigger an error under certain conditions (using trigger_error()).

What about undefined functions in exception handler callbacks?

Let's suppose I have this piece of code, where I try to make sure all my errors are reported in some way, yet the visitors won't have to see but a nice page apologizing for the situation.
ini_set('display_errors', 'off');
error_reporting(E_ALL);
set_exception_handler('exceptionHandler');
function exceptionHandler($error)
{
// functionWithSyntaxError()
undefinedFunction();
echo $error->getMessage();
}
If I would have functionWithSyntaxError() uncommented, I would see the error about the syntax. I guess that's because the code is not even run and the compiler doesn't care about my exception handler or other directives.
Now, if I comment it back and leave only undefinedFunction(), I wouldn't be able to log the error about the undefined function, either would my code run further. I wouldn't know what happens so I would have to set display_errors ON, in which case I would defy my original purpose of not displaying errors, but reporting them silently.
So, in this case I guess the compiler doesn't check for undefined function as it does with the syntax. What happens in this case? It certainly doesn't continue either. Shouldn't the code go in a loop? What happens under the hood?
I know I have better options to handle errors gracefully, defining a debug mode (where errors will be displayed), for example, but I just want to understand the intricacies of this situation

HTTP Error 500 when running php scripts

I am running a PHP page and as soon as I introduce calls like this: $_GET('') then everything goes wrong and I get an error 500.
This code goes not work:
echo $_GET('username');
echo $_GET('password');
?>
This code does:
<?php
phpinfo();
?>
The above code has syntax errors - you need to use square brackets.
The web server's error logs will show you those errors if you have access to them.
Use this:
echo "Username: ".$_GET['username']."<br />Password: ".$_GET['password'];
Since $_GET is a array and not a function, you need to use [square brackets] instead of (normal brackets) to retrieve the data out of a array.
To figure out what the problem is you need to turn on php error reporting. You do this by running this the first thing you do in your php-file:
ini_set('display_errors',1);
error_reporting(E_ALL);
E_ALL means the interpreter will show you errors, warnings and notices. After that, everything will be pretty obvious since php will tell you what went wrong.

PHP custom error page

Everyone says that "Enabling errors to be shown" in an active site is bad (due to some security issues).
Now, we have to consider 2 cases:
The site is in debug mode
The site is not in debug mode
Now, for case #1:
We want to see the errors. How?
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
Nothing more simple. Also we can customize an error handler for all errors except Parse and Fatal.
Instead, if the case is #2:
We would like to be able to deactivate the messages:
ini_set('error_reporting', 0);
ini_set('display_errors', 0);
And it's ok. But what about showing users a friendly message such as "Hei man, something is really f**ked up. I don't assure you we are working to fix it, since we are very lazy.".
You should enable errors again and just use the function set_error_handler() and hope that no parse or fatal errors occur. But my first question is:
Question 1: Is that possible to avoid error reporting and have a custom offline page that is loaded when something goes wrong? I mean, is it possible to have ini_set('error_reporting', 0); and ini_set('display_errors', 0); and still be able to tell PHP to load a custom Error page?
And now another:
Question 2: I developed a class that with the power of set_error_handler() logs errors occurred into the database. In this way I can keep track of hack attempts and other cool stuff. (And yes, i'm always sure the DB is accessible since my application shuts down if we cannot connect to the DB). Is this worth something?
Some time ago I created small system that redirects you to error page when fatal error occurs / uncaught exception was thrown. It was possible with assumption, that every request is handled by one file and ends in this file, so by reaching end of this file I'm sure that everything went OK. With this condition I've set up function to redirect on error page and registered it as shutdown function - so it will be called at the end of all requests. Now in this function I check conditions for clean shutdown and if hey are met, I do nothing and output is flushed to the browser, otherwise buffer is cleaned and only header redirecting to error page is sent.
Simplified version of this code:
<?php
function redirect_on_error(){
if(!defined('EVERYTHING_WENT_OK')){
ob_end_clean();
header('Location: error.html');
}
}
register_shutdown_function('redirect_on_error');
ob_start();
include 'some/working/code.php';
echo "Now I'm going to call undefined function or throw something bad";
undefined_function();
throw new Exception('In case undefined function is defined.');
define('EVERYTHING_WENT_OK', TRUE);
exit;
Question 1: Is that possible to avoid error reporting and have a custom offline page that is loaded when something goes wrong?
Unfortunately, I don't think so, at least for fatal errors. However, recent versions of PHP always send a 500 response when that occurs, so, depending on webserver, you may be able to rewrite the response if such thing happens. If your actual server running PHP is behind a reverse proxy, this becomes trivial with Apache.
Question 2: I developed a class that with the power of set_error_handler() logs errors occurred into the database.
Sure, it's always good to log the errors. You already seem to be aware of the limitations of logging errors into the database.

PHP turn off errors - in one file only

I am well aware about error_reporting(0); & ini_set('display_errors', "Off"); to make error messages go away.
What would be an appropriate way to do this - for a specific file or part of code only?
Surpressing errors with #'s seems like a bad idea since it apparently slows the code down...
The reason? We have a number of memcached servers in a development LAN that is really unreliable due to the network settings, thereby we are recieving errors multiple times every hour and there's nothing we can do about it except stop using memcache or turning off errors for the whole application, which would be giving us a headache - in the middle of the development stage :)
<?php
// normal code
// error_reporting returns the old error code
$old_error_reporting = error_reporting(0);
// your errorful code
// reset error_reporting to its old value
error_reporting($old_error_reporting);
// normal code
Although it would be a good idea to fix what is actually causing the errors.
You've kind of answered your own question. To do it for a specific file, error_reporting(0); will turn off errors. You can also call it multiple times in a script, I think.
You can also use php exceptions to 'catch' errors over a block of code. For example:
try {
// code to ignore errors for here
} catch {
// you can output a custom error here, but putting nothing here will effectively mean errors are ignored for the try block
}
The script will continue running past the try block, even if there is an error within it. See the PHP Manual Entry for more information.
You can change the error reporting level during runtime:
<?
error_reporting(E_ALL);
... some code ....
error_reporting(0);
... some more code ....
error_reporting(E_ALL);
I know of no other way but I can't think of a case where this wouldn't be sufficient. Can you?
That's really a long time ago but someone like me would maybe use my answer.
When i need to do this kind of stuff, i just put # before the variable in order to NOT display the errors coming from this variable.
example:
switch(#$var!="e") {
....
}

Categories