Redirect to specified URL on PHP script completion?


Question

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');  
?>
1
35
4/4/2014 8:54:45 PM

Accepted Answer

<?
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" );
?>
64
12/9/2008 8:49:08 PM

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon