开发者

PHP/Apache - how to see script warnings before header redirects?

I use a lot of header redirects but because of that I miss some PHP warnings that don't get shown due to redirect. Is there some PHP/Apache configuration option that would stop header redirects if any warnings are issued?

In fact I would like any warnings to stop script execution (like errors do) on my development machine but to be invisible on other servers - so that means some configuration setting and not changing of scripts. Is that possible? I'm using Apache 2.2 and PHP 5.2

All warnings are displayed normally on my development server, but when they're followed by a header redirect, I can't see them. Example:

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
unset($undef);
echo $un开发者_JAVA技巧def; // generates warning, should stop redirect.
//exit; 
//when previous line is uncommented, I see the warning
header("Location: http://www.google.com"); // still happens, but shouldn't
exit;
?>


On your development servers put:

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 'On');
ob_start();
...

if(ob_get_length() > 0){
    ob_flush();
    exit();
}
header(...);

before your header redirects (as shown). If anything is in the buffer it will be displayed and this will cause your header redirect to fail because it is impossible to do a header redirect if something has been rendered to the browser. If you turn display_errors to Off on your production servers, then these messages will not display.


Do something like this:

//header("location: such-and-such");

lol, but really; its not more simple than that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜