Response.Redirect (ASP) in PHP after outputting some HTML?

In ASP I can buffer the response of the headers and html until I choose to sent it.
-->Response.Buffer = True

I googled and found: Header("Location:\n\n"); \

But the question is....How can I send this header AFTER displaying some HTML?  Is there a setting for PHP that I can easily cache the output until the page is done processing?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Just print this to the html:
// -->

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
djlurchAuthor Commented:
If possible, I would rather do it through the headers than through Javascript.  Good suggestion though.
The headers can only work before outputting anything into the html.
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

djlurchAuthor Commented:
Can someone with lots of EE PHP points confirm this?'ll still get some points
In php you cannot change headers after sending some output,
only way is using the javascript. no way using php.
There is also such a option in PHP. Use output-buffering See So there is a way to send headers, even if there has been output before.

Use it like this:
<?php ob_start(); ?>
<?php header("Location:"); ?>


apg88 and str_kani  are both right, you cannot send a header once there is an HTML output sent to the browser, even a blank space will give errors.
I would like to provide you a PHP Javascript combination code for redirecting without using header();


      function redirect($url)
            echo "<script language='javascript'>\n";
            echo "function redirect()\n";
            echo "{\n";
            echo 'window.location = "'. $url .'"';
            echo "\n}\n";
            echo "redirect();\n";
            echo "</script>\n";

redirect (""); //dont have to be direct URL can be relative paths as well

If you examine the full HTTP packet that is sent to a users browser in response from a web server you will see it goes:


In systems like PHP/ASP etc as soon as you send any content to the html-body the app server writes the HTML headers. However, in ASP, Response.Buffer defaults to True in ASP3.0 on IIS5 or better. This means that what ACTUALY happens is you write to a string which is buffered internaly by the ASP dll until page generation finishes, THEN it writes it to the atual output stream, triggering header creation. So you can response.redirect any time you like (unless Response.Buffer is false)

The same can be acheived in PHP using the output buffering features linked by hernst42. However, you MUST make sure that ob_start(); is called before ANYTHING is output to the page, including blank space prior to the first PHP tags


header("location: page.php");

Will fail as the blank space prior to <?php will trigger http headers to be written.

//page logic
echo('here is some html');
more html
//more page logic
if( $redirect )
  header('location: page.php'); // this will still work
//more page logic
echo('here is some more html');
ob_end_flush(); // write buffers to page, must call this to cleanly output content and sort everything out.
djlurchAuthor Commented:
Thank you!

Eye: good works great!

Every page has an include I just put it the buffer call up top and the flush at the only 2 lines of code need to be changed for the whole site.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.