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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/353803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:28:49  来源:igfitidea点击:

Redirect to specified URL on PHP script completion?

phpurl

提问by JoshFinnie

How can I get a PHP function go to a specific website when it is done running?

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

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

回答by Patrick Hogan

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

回答by user44856

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

您总是可以使用标记来刷新页面 - 或者可能只是在最后将必要的 javascript 放入页面中,这会导致页面重定向。你甚至可以把它放在一个 onload 函数中,所以一旦它完成,页面就会被重定向

<?php

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

回答by Bullines

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

如果“已完成”没有通过 echo/print/etc 调用任何输出,则:

<?php
   // SOMETHING DONE

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

回答by FryGuy

Note that this will not work:

请注意,这将不起作用:

header('Location: $url');

You need to do this (for variable expansion):

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

header("Location: $url");

回答by Aistina

<?php

// do something here

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

回答by as_bold_as_love

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].

b:我假设您不希望页面上的其余代码运行,这就是为什么您首先将此重定向放在首位 [也许]。

example:

例子:

<?php

// do something here

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

//code down here now wont get run

?>

回答by Paul M

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 代码是您页面上的第一件事……在任何 doctype 和 head 标签以及所有爵士乐之前。然后,当 POST 返回页面时,php 代码将首先出现并且不会遇到已发送的标头问题。