views:

92

answers:

2

Hi all! When i do a redirect, no errors are shown. I use codeigniter framework and there's

error_reporting(E_ALL);
ini_set('display_errors', 1);

in my index.php file. When something is breaking in my code(e.g.some 'undefined index' error) and i do a redirect to another page - the error isn't showing. How to avoid this - break the redirect and show the errors?

redirect function is just a simple header:

header('Location: ' . $uri);

I'm assuming this is something connected with output buffers. I've tried to turn on buffer(in index.php) using ob_start() but nothing happens.

Thanks.

+1  A: 

The approach I take it to use a custom error handler which throws exceptions on any error; this makes sure I don't miss anything during development. Throwing the exception will cause your code to abort immediately, therefore avoiding the redirect. For the production sites, I disable this (and also disable display_errors, of course). I've included simple code to do this below.

/**
 * Function to throw exceptions on any errors.
 */
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    // Ignore suppressed errors
    if (error_reporting() == 0) { 
        return; 
    }
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}

// Report ALL errors
error_reporting(E_ALL | E_STRICT | E_NOTICE);
// Enable error display
ini_set('display_errors', 1);
// Set the error handler
set_error_handler('exception_error_handler');

Note that this should be done as early as possible in the execution of your script, to catch all errors. Note also that the error_reporting() call may cause too many errors to be reported for your liking; we develop to a very strict standard and I find that throwing exceptions even for E_NOTICE type errors saves me a lot of pain down the line, but it's up to you.

El Yobo
Thanks, but it doesn't work for me :(exception_error_handler() even doesn't call for me(i've tried die() in the beginning of it). As i mentioned in the question - i don't see 'notice' errors...//p.s i've tried some code and it seems that it doesn't show even warning msgs.
DCrystal
Codeigniter may install its own error handler - see this for more information - http://codeigniter.com/user_guide/general/errors.html
El Yobo
Thanks again, i just forgot about this...
DCrystal
+1  A: 

Within your application/config/config.php file you need to set the error reporting level to catch php errors.

Once this option is on all php errors should be logged within the system/logs folder.

Because the redirect function uses a header redirect no content (even errors) are sent to the browser, otherwise your redirect would stop working.

Edit: As far as I can see you want to do 1 of 3 things:

Option 1 Display the error and not do any redirect at all. If this is the case just remove the redirect function from your error handler.

Option 2 Display the error but redirect after a period of time. If this is the case your looking at doing a different type of header redirect:

header("refresh:5;url=/somewhere/else");

This will after 5 seconds redirect the browser to somewhere/else.

Option 3 Conditionally do the redirect (if in development show the error, if not in developement then don't show the error).

To do this add a conditional statement to the Code Igniter error handler to test if you are in developement mode, or check your ip and then do the redirect depending on these.

Is this the kind of thing you are looking to do? Or is it more like only redirect if there was no php error? If so, where are you calling the redirect function from?

Ian Cant
I mentioned in the question that i use error_reporting(E_ALL) in index.php. Loggin in files isn't appropriate way in my case - because i want to see the errors while testing the things.The main aim is to stop redirect and @EI Yobo proposed to use exceptions for this - though i don't think it's the best way and i still sure that it's smth with the output buffering...
DCrystal
Yes, i want to do a redirect only if there were no errors.Redirect function is calling from controller.For now i just add conditions to redirect function and if ob_get_contents() not empty -> i do not do redirect..
DCrystal