Jump to content

Redirect help


lard

Recommended Posts

  Quote

 

 

/ 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. 

Link to comment
https://forums.phpfreaks.com/topic/290970-redirect-help/#findComment-1490588
Share on other sites

  On 9/10/2014 at 11:56 AM, lard said:

 

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.

Link to comment
https://forums.phpfreaks.com/topic/290970-redirect-help/#findComment-1490862
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/290970-redirect-help/#findComment-1490942
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.