Jump to content

Redirect help


lard

Recommended Posts

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");
?>
Link to comment
Share on other sites

 

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 by Masna
Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.