How can I get a PHP function go to a specific website when it is done running?
For example:
Note that this will not work:
header('Location: $url');
You need to do this (for variable expansion):
header("Location: $url");
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'</script>";
?>
If "SOMETHING DONE" doesn't invovle any output via echo/print/etc, then:
<?php
// SOMETHING DONE
header('Location: http://stackoverflow.com');
?>
<?
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" );
?>
<?php
// do something here
header("Location: http://example.com/thankyou.php");
?>
Here's a solution to the "headers were already sent" problem. Assume you are validating and emailing a form. Make sure the php code is the first thing on your page... before any of the doctype and head tags and all that jazz. Then, when the POST arrives back at the page the php code will come first and not encounter the headers already sent problem.