lard Posted September 10, 2014 Share Posted September 10, 2014 Can anyone enlighten me on why this doesn't redirect and if there's a better / different way of achieving a redirect deep within a page? <?php echo"anything"; // remove this echo or even make it blank and it redirects as expected header("location:blah.php"); ?> Quote Link to comment Share on other sites More sharing options...
gristoi Posted September 10, 2014 Share Posted September 10, 2014 you cannot have any output prior to a header Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 10, 2014 Share Posted September 10, 2014 (edited) Is there something specific you are trying to accomplish...or are you just wondering about the header() function? Edited September 10, 2014 by cyberRobot Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted September 10, 2014 Share Posted September 10, 2014 / different way of achieving a redirect deep within a page? You need to reformat your code. Business logic should not be mixed in with your HTML. If you are using header after output has been made you need rethink your logic. An absolute last resort would be to use output buffering or use the HTML meta redirect tag. Quote Link to comment Share on other sites More sharing options...
Masna Posted September 12, 2014 Share Posted September 12, 2014 (edited) Can anyone enlighten me on why this doesn't redirect and if there's a better / different way of achieving a redirect deep within a page? <?php echo"anything"; // remove this echo or even make it blank and it redirects as expected header("location:blah.php"); ?> Once anything is outputted by a php page, php flushes the buffered implied headers (auto-generated unless explicitly defined otherwise, things like Content-Type) and starts generating a response for the HTTP request. Because of echo "anything";, you are forcing this to happen. Thus, any headers you try to set thereafter (however you do it, including using the header() function) are rendered useless. It'd be like trying to set an accept encoding in your POST body - it just makes no sense. Edited September 12, 2014 by Masna Quote Link to comment Share on other sites More sharing options...
LeJack Posted September 13, 2014 Share Posted September 13, 2014 That's weird because when I tried it on my localhost, it works just fine. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 13, 2014 Share Posted September 13, 2014 That's because you have output buffering enabled on your localhost. Either way, the structure is wrong. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 13, 2014 Share Posted September 13, 2014 if php's output_buffing is on prior to any output being produced, the output is buffered instead of being sent to the browser and header() statements will function. php's output buffering can be turned on in the php.ini on your system. this is not the ideal situation, as it hides things like php error messages and messages your application intentionally displays, and results in code that is not portable between systems. it's always best to NOT rely on any sort of setting like output_buffing when developing code so that your code is properly structured and will work on the largest number of systems. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.