No previous output,but header redirect doesn't work in PHP
header('Location: ' . $url);
I've checked that there is no previous output:
ob_start();
...
var_dump(ob_get_contents());
ob_flush();
header('Location: ' . $url);
outputs:
string '' (length=0)
Then why does the redirect fail?
Though I see lots o开发者_如何学Cf warnings and notices in the error_log
,but that doesn't affect header()
as long as it doesn't output anything to the browser,right?
UPDATE
Some logs(should not be related though):
[Wed May 19 00:26:10 2010] [error] [client 127.0.0.1] PHP Deprecated: Function eregi() is deprecated in D:\\Works\\general
[Wed May 19 00:26:10 2010] [error] [client 127.0.0.1] PHP Stack trace:, referer: http://localhost/cookie_usage.php
[Wed May 19 00:26:10 2010] [error] [client 127.0.0.1] PHP 1. {main}() D:\\Works\\login.php:0, referer: http://localhost/cookie_usage.php
[Wed May 19 00:26:10 2010] [error] [client 127.0.0.1] PHP 2. tep_redirect() D:\\Works\\login.php:33, referer: http://localhost/cookie_usage.php
ob_flush() sends to the output buffer. (prints what is in the OB) That is considered output. you may want to use ob_clean()
it is also good practice to use exit() or die() after a header('location ...') call.
Also note that whitespace at the end of the file will do this.
For the most part I never use ?> at the end of a PHP file anymore, just leave it open
?> is optional anyway.
I was facing similar issue before few days. There was extra white space in the end of php included file which I was calling before header function. I removed that white space and it starter working for me.
that doesn't affect header() as long as it doesn't output anything to the browser,right?
wrong.
精彩评论