在PHP脚本完成时重定向到指定的URL?

Jos*_*nie 35 php url

如何在运行完成后将PHP函数转到特定网站?

例如:

<?php
  //SOMETHING DONE
  GOTO(http://example.com/thankyou.php);
?>
Run Code Online (Sandbox Code Playgroud)

我真的很喜欢以下......

<?php
  //SOMETHING DONE
  GOTO($url);
?>
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情:

<?php
  //SOMETHING DONE THAT SETS $url
  header('Location: $url');  
?>
Run Code Online (Sandbox Code Playgroud)

Pat*_*gan 65

<?
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" );
?>
Run Code Online (Sandbox Code Playgroud)


use*_*856 33

您可以随时使用标记刷新页面 - 或者只是将必要的javascript放到页面末尾,这会导致页面重定向.你甚至可以在o​​nload函数中抛出它,所以一旦完成,页面就会被重定向

<?php

  echo $htmlHeader;
  while($stuff){
    echo $stuff;
  }
  echo "<script>window.location = 'http://www.yourdomain.com'</script>";
?>
Run Code Online (Sandbox Code Playgroud)


Bul*_*nes 14

如果"SOMETHING DONE"没有通过echo/print/etc调用任何输出,则:

<?php
   // SOMETHING DONE

   header('Location: http://stackoverflow.com');
?>
Run Code Online (Sandbox Code Playgroud)


Fry*_*Guy 5

请注意,这不起作用:

header('Location: $url');
Run Code Online (Sandbox Code Playgroud)

您需要这样做(用于可变扩展):

header("Location: $url");
Run Code Online (Sandbox Code Playgroud)

  • 或者,当然:标题('Location:'.$ url); (2认同)