Redirection After HTML OutPut

HI Gurus,

Is there any method available by which i can REDIRCET to another page anywhere in the PHP after HTML output ??? I have seen several solutions with PHP + Javascript… is there any way t do this 100% in PHP ?? Becasue there are browsers which do not suppors JavaScript, or the USer has just DISABLED JavaScript ??? Please help

Use this:


<?php

header("Location: www.pageyouwanttogotohere.com");

?>

If you want any code below this header function to be ignored, simply add exit after it.


<?php

header("Location: www.pageyouwanttogotohere.com");

exit;

?>

For more information, visit:

PHP: header

Sounds like you might be talking about redirection after there has already been output, like:


<html>
some output
<?php
if ( $some_condition )
{
  header( 'Location: http://www.google.com' );
  exit;
}
?>

If so, PHP has Output control functions which allow you to control when the output (such as HTML code) is sent to the browser:


<?php
ob_start( );
?>
<html>
some output
<?php
if ( $some_condition )
{
  ob_end_clean( );
  header( 'Location: http://www.google.com' );
  exit;
}
?>
some output
</html>
<?php
ob_end_flush( );
?>

You can check out more at: http://us3.php.net/manual/en/ref.outcontrol.php

If you are talking about redirecting after the page has been rendered you can not do this with PHP, as PHP is on the server and runs before the page is rendered. If you want the page to redirect based on user inter-action then you can use javascript (or maybe vbscript too?). If you simply want a timed redirect you can use a meta tag eg.

<meta http-equiv="refresh" content="2;url=http://www.domain.com/">

Where “2” is the time in seconds before the page is reloaded.

I think you’re right. I mis-read his problem. Well done. :slight_smile:

you can use javascript. This little function below works great:


function js_redirect($url, $seconds=1)
			{
				echo "<script language=\\"JavaScript\\">\
";
				echo "<!-- hide from old browser\
\
"; 	
				echo "function redirect() {\
";
				echo "window.location = \\"" . $url . "\\";\
";
				echo "}\
\
";
				echo "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\
\
";
				echo "-->\
";
				echo "</script>\
";
				return true;
			}


just call it after you echo your output like this:


echo "Your html here";
js_redirect ('page to which you want to redirect', time html above is displayed in seconds before redirect);


I think he stated in his original post that he wanted to avoid Javascript.

That’s an interesting question, and one I’ve struggled with many times my self. Yes I too try and avoid Javascript redirects on public pages, using a php header redirect after output doesn’t work, and yes simple meta redirecting certainly has downsides. I go the meta refresh route, I just make it a decent length of time, so the switch is not super abrupt, and also place an href link so users do not have to wait if they do not want to. Really clumsy, and if someone has another solution, I’d love to hear it.

Yes there are issues with the meta tag. This is apparent particularly on websites where you are transfering confidential informatin across the internet, for example, buying things. If you are using the meta tag, be sure you give the user at least 10 seconds, and give them the option (via a link) to skip the refresh and go straight to the next page. It will give them more confidence.

HI Participents. Thanks for your replys here.
I would like to add some thing. People, in ASP we have “resonse.redircet…” which i think works even after the HTML OUTPUT. And uptill now, ( I HAVE A VERY LIL KNOWLEDGE OF THIS) but I feel that PHP is much STRONGER than ASP. Than WHY PHP DO NOT HAVE SUCH FACILITIES ??? Moreover, I have got only 1 response with “ob_start();” is that a GOOD SOLUTION ???

I was the only one who mentioned ob_start as everyone was unsure quite what you were asking.

Output buffering was designed to solve the problem of sending headers (e.g. header location redirects) even after “sending” output to the browser… From PHP.net:

The Output Control functions allow you to control when output is sent from the script. This can be useful in several different situations, especially if you need to send headers to the browser after your script has began outputting data. The Output Control functions do not affect headers sent using header() or setcookie(), only functions such as echo() and data between blocks of PHP code.

Try putting output buffering into your script and see if that solves the problem you were having. Output buffering makes it so that your output is sent at the end of your script’s execution as opposed to when it is generated during the execution.

I would say that actually ASP is “stronger” than PHP. It is the language of choice for corporate intranets where typically you have a great deal of web based applications.
However for internet I think PHP much more suitable, it is much easier on server system resources, with the upgrades in PHP5, it is giving ASP some real competition, but I still say using ASP for internet stuff is akin to using a shotgun to kill flies, it will do it, but it’s unnecessarily powerful.
Moreover you received almost a dozen responses and as you say only one response with “ob_start();”. If you think that is the only solution of any merit, you seem to have a good idea about the answer to your own question, so why did you ask it in the first place? It certainly sounds like the best solution, but certainly not the only one offered. Perhaps it was only mentioned by one person, because only one person is familiar with it. If you’re going to display such a massive level of ingratitude, I would strongly recommend sticking to what you know!

Also, you might look into flush() and sleep().
Just an idea.

Using the buffer functions is unavoidable in some situations. As a “buffer” uses memory resources (probably not that big of a deal) I try to avoid using it unless absolutely necessary. For example, intead of

<?php
ob_start();
......
<html>
......
<body>
.....
<?php
if (!what_I_want){
  ob_end_clean( );
  header( 'Location: http://www.google.com' );
.....

I would write the code more like this

<?php
if (!what_I_want){
  header( 'Location: http://www.google.com' );
.....
else {
?>
<html>
.....

I store most of the time my HTML output in a $variable and echo at the bottom of the php code…

Al redirections and headersending is executed before the actual html echoing.

wich means I don’t need the ob flush() stuff…

Is there in a way an advantage in using the ob_fush(); instead of doing as I explained?

greetz,
Koen

 <?php
ob_start();
echo "<script>document.location.href='http://yoururlhere?var=yourvar'</script>";
}
ob_end_flush(); ?>

no meta data at all! :slight_smile:

Thank you all. You all have great knowlegde, and its great and wonderfull to have such a comprehensive help. Thanks once again.