jdhackett
asked on
PHP Redirect not working
Hi
I had this code which worked one website.
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
I had this code which worked one website.
header('Location: tm_wufoo.php');
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)
What must I do to get rid of this warning?
Thanks
You could get around this using javascript.... like
A better way is to put it all in one tag of possible though.
<?php
function MyJavaScriptRedirection($url)
{?>
<script type=”text/javascript”>
<!–
window.location = “<?=$url?>”
//–>
</script>
<?}
print “do stuff”;
MyJavaScriptRedirection(“tm_wufoo.php”);
?>
A better way is to put it all in one tag of possible though.
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you might try changing the line to
header('Refresh: 1; url=http://yoursite.com/PATHtoFILE/tm_wufoo.php');
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks everyone. Ob_start solved the issue.
Is it considered "bad practice" that I had to do this?
Is it considered "bad practice" that I had to do this?
No, that's what it's there for.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER
Thanks everyone for their help, very educational.
Open in new window