41

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

7 Answers 7

71
<?
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" );
?>
4
  • 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(). Dec 9, 2008 at 19:08
  • 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
    Dec 9, 2008 at 22:07
  • 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 Dec 9, 2008 at 22:27
  • Should no redirect be now redirect? Dec 8, 2021 at 3:27
42

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>";
?>
2
  • This worked nicely for me - Headers were already sent due to lots going on in the CMS area, this was perfect
    – Simon
    Nov 29, 2011 at 16:05
  • Simple and brilliant, thanks for this useful answer!
    – SuN
    Mar 19, 2015 at 22:41
15

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

<?php
   // SOMETHING DONE

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

Note that this will not work:

header('Location: $url');

You need to do this (for variable expansion):

header("Location: $url");
0
5

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

?>
3
<?php

// do something here

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.