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 ju开发者_如何转开发st 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.
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.
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?
By default, you get a "Cannot modify header information - headers already sent...". If you're not getting this, it means there's a custom error handler involved.
To make redirects stop when an error is encountered, either remove the error handler altogether, or call flush() at the end of it:
function my_error_handler($errno, $errstr, $errfile, $errline) {
// error handling code
flush();
}
set_error_handler('my_error_handler');
精彩评论