tags:

views:

3308

answers:

5

How can I get a PHP function go to a specific website when it is done running?

For example:

<?php

SOMETHING DONE

GOTO(http://example.com/thankyou.php);

?>

I know this is really simple, but I am just having a hard time finding it on the interweb!

Update: I guess I should have mentioned this. I would really like the following...

<?php

SOMETHING DONE

GOTO($url);

?>

Would this work with as the following?

<?php

SOMETHING DONE THAT SETS $url

header('Location: $url');

?>
+4  A: 

If "SOMETHING DONE" doesn't invovle any output via echo/print/etc, then:

<?php
   // SOMETHING DONE

   header('Location: http://stackoverflow.com');
?>
Bullines
+1  A: 
<?php

// do something here

header("Location: http://example.com/thankyou.php");
?>
Aistina
+9  A: 
<?
ob_start(); // ensures anything dumped out will be caught

// do stuff here
$url = 'http://example.com/thankyou.php'; // this can be set based on whatever

// clear out the output buffer
while (ob_get_status()) 
{
    ob_end_clean();
}

// no redirect
header( "Location: $url" );
?>
pbhogan
You beat me to it. It might be worth mentioning that, since headers can only be sent if no output has been generated, the whole page/application might need to be wrapped in an ob_start().
Sean McSomething
When I tried this, I received this error:"Warning: Cannot modify header information - headers already sent by (output started at example.php:1)" Do you know why this would be happening?
JoshFinnie
This means you're outputting something before ob_start() or after the ob_end_clean() block. Output buffering (the ob_* functions) take care of buffering any output allowing you to use it or (as in this case) discard it later. Put your code at //do stuff here
pbhogan
+3  A: 

Note that this will not work:

header('Location: $url');

You need to do this (for variable expansion):

header("Location: $url");
FryGuy
Or, of course: header('Location: ' . $url);
Hexagon Theory
+1  A: 

You could always just use the tag to refresh the page - or maybe just drop the necessary javascript into the page at the end that would cause the page to redirect. You could even throw that in an onload function, so once its finished, the page is redirected

<?php

  echo $htmlHeader;
  while($stuff){
    echo $stuff;
  }
  echo "<script>window.location = 'http://www.yourdomain.com'&lt;/script&gt;"
?>
Why didn't I think of that! Thank you for thinking outside the box...
JoshFinnie