Link to home
Start Free TrialLog in
Avatar of jdhackett
jdhackettFlag for Ireland

asked on

PHP Redirect not working

Hi
I had this code which worked one website.
header('Location: tm_wufoo.php');

Open in new window


So it redirected to the tm_wufoo page once certain conditions were met.
It doesn't work on the new site though. Instead I get the message:
Warning: Cannot modify header information - headers already sent by (output started at blah.org\tm_header.php:25) in blah.org\tm_login.php on line 31

What must I do to get rid of this warning?
Thanks
Avatar of StingRaY
StingRaY
Flag of Thailand image

Try inserting "exit();" after redirect like this:

header('Location: tm_wufoo.php');
exit();

Open in new window

You could get around this using javascript.... like

<?php
function MyJavaScriptRedirection($url)
{?>
<script type=”text/javascript”>
<!–
window.location = “<?=$url?>”
//–>
</script>
<?}
print “do stuff”;
MyJavaScriptRedirection(“tm_wufoo.php”);

?>

Open in new window


A better way is to put it all in one tag of possible though.
Avatar of jdhackett

ASKER

Stringray - added exit() but it made no difference. Are there PHP admin settings that could account for the difference in the way the code works?
Avatar of Dave Baldwin
Any redirect with header('Location: tm_wufoo.php'); must come BEFORE any output to the browser.  Even a space ' ' will cause the other headers to be sent and block sending anymore.  There are no settings which can change that, it's part of the HTTP protocol.
ASKER CERTIFIED SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you might try changing the line to

header('Refresh: 1; url=http://yoursite.com/PATHtoFILE/tm_wufoo.php');

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks everyone. Ob_start solved the issue.
Is it considered "bad practice" that I had to do this?
No, that's what it's there for.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I generally agree with jrm213jrm213 about the logic that would begin producing browser output before it had completed all of the logic needed to build the page.  Life works better, your scripts are more maintainable that way, etc.

But in my experience the observed behavior of using ob_start is that it makes your scripts run faster.  Instead of connecting and sending several chunks of browser output, it saves all the chunks together in one big chunk until the script terminates, and then sends it all at once.  There is considerable overhead associated with sending each chunk of browser output, thus reducing the number of chunks can reduce the total overhead.
Thanks everyone for their help, very educational.