[php] Redirect to specified URL on PHP script completion?

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 would really like the following...

<?php
  //SOMETHING DONE
  GOTO($url);
?>

I want to do something like this:

<?php
  //SOMETHING DONE THAT SETS $url
  header('Location: $url');  
?>

This question is related to php url

The answer is


<?
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" );
?>

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.


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>";
?>

<?php

// do something here

header("Location: http://example.com/thankyou.php");
?>

Note that this will not work:

header('Location: $url');

You need to do this (for variable expansion):

header("Location: $url");

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

<?php
   // SOMETHING DONE

   header('Location: http://stackoverflow.com');
?>

Note that this will not work:

header('Location: $url');

You need to do this (for variable expansion):

header("Location: $url");

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

<?php
   // SOMETHING DONE

   header('Location: http://stackoverflow.com');
?>

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>";
?>

Note that this will not work:

header('Location: $url');

You need to do this (for variable expansion):

header("Location: $url");

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

<?php
   // SOMETHING DONE

   header('Location: http://stackoverflow.com');
?>

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.


<?php

// do something here

header("Location: http://example.com/thankyou.php");
?>

Note that this will not work:

header('Location: $url');

You need to do this (for variable expansion):

header("Location: $url");

don't forget to put a 'die' after your call to make the redirect happen before the rest of the code on the page is run threw. a. if you have header functions further down the page they will override the ones further up the code.

b: im assuming you dont want the rest of the code on the page to be run and that why your putting this redirect in in the first place [maybe].

example:

<?php

// do something here

header("Location: http://example.com/thankyou.php");
die();

//code down here now wont get run

?>

<?php

// do something here

header("Location: http://example.com/thankyou.php");
?>

don't forget to put a 'die' after your call to make the redirect happen before the rest of the code on the page is run threw. a. if you have header functions further down the page they will override the ones further up the code.

b: im assuming you dont want the rest of the code on the page to be run and that why your putting this redirect in in the first place [maybe].

example:

<?php

// do something here

header("Location: http://example.com/thankyou.php");
die();

//code down here now wont get run

?>

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

<?php
   // SOMETHING DONE

   header('Location: http://stackoverflow.com');
?>

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>";
?>