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: http://123.456.com\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?
LVL 1
djlurchAsked:
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.

apg88Commented:
Just print this to the html:
<SCRIPT LANGUAGE="JavaScript">
<!--
window.location="http:/http://123.456.com";
// -->
</script>
0

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.
0
apg88Commented:
The headers can only work before outputting anything into the html.
0
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?  apq...you'll still get some points
0
str_kaniCommented:
In php you cannot change headers after sending some output,
only way is using the javascript. no way using php.
0
hernst42Commented:
There is also such a option in PHP. Use output-buffering See http://de3.php.net/manual/en/ref.outcontrol.php So there is a way to send headers, even if there has been output before.

Use it like this:
------test.php-------------
<?php ob_start(); ?>
text
<?php header("Location: http://123.456.com"); ?>
sadsdas

0
minichickenCommented:
Hi

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();

Example:

********************************************************************
<?
      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 ("http://www.mydomain.com"); //dont have to be direct URL can be relative paths as well
?>

regards-
0
eyeh8uCommented:
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:

(http-headers)
(html-body)

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

----

<?php
ob_start();
header("location: page.php");
?>
---

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

---
<?php
ob_start();
//page logic
echo('here is some html');
?>
more html
<?php
//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.
?>
---
0
djlurchAuthor Commented:
Thank you!

Eye: good call...it works great!

Every page has an include file...so I just put it the buffer call up top and the flush at the closing....so only 2 lines of code need to be changed for the whole site.
0
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
PHP

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.