tags:

views:

188

answers:

3
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 of 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
+3  A: 

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.

DBruns
He's only using ob here to test for output.
webbiedave
I didn't realize that, however it is a solution to the problem; Use ob to capture the output and ob_clean() and the redirect should work, otherwise there may be whitespace outside of <?php tags somewhere
DBruns
I also tried `ob_clean()` before `header`,not working.And the url is valid.
Remove the ?> at the end of your code if you have one there
DBruns
There was no `?>` at the end of code.
try doing a header('location: http://www.google.com'); exit(); instead and see what happens. If it redirects, something in your header redirect is bad, otherwise you do have output going on somewhere. Maybe its in an included or required file. Could you maybe put the entire php file on pastie.org (or similar) for us?
DBruns
A: 

that doesn't affect header() as long as it doesn't output anything to the browser,right?

wrong.

Col. Shrapnel
Why?PHP manual doesn't mention it.
Because you have to read the error message, not sitting and guessing all day! If it's header's problem there ought to be an error message. To be cartain, what is wrong. Playing with output buffering is just wrong way.
Col. Shrapnel
I'm not guessing and I check error logs frequently,but this time it really doesn't help!
@user if there is no header error, there is **not a single reason** to play with ob_start. It must be sensible action, based on some reason (an error message). So, if you check your buffering without a reasom, I call it guessing. Because you *guess* there is some header error, not knowing it. Now you have to check the $url variable and code logic - if this header get ever executed
Col. Shrapnel
I saw it got executed by step-by-step debug,but somehow there is no Location header in the response.
@user that seems very strange. You have tp double check for the "Headers already sent" error. You may be need to lower errorl_reporting to warning level
Col. Shrapnel
A: 

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.

CM