通过 PHP 更改 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2764301/
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-25 07:37:23  来源:igfitidea点击:

Change the URL through PHP

phpurl

提问by Nitz

I want to change the URL through PHP.

我想通过 PHP 更改 URL。

Something like window.location.href = "http://www.something.com"in JavaScript.

类似于window.location.href = "http://www.something.com"JavaScript 中的东西。

I want the same thing but in PHP. How can I do it?

我想要同样的东西,但在 PHP 中。我该怎么做?

回答by Sarfraz

You can use the headerfunction for that:

您可以使用该header功能:

header("LOCATION: http://www.something.com");

回答by richsage

You could use the header() command:

您可以使用 header() 命令:

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

回答by Glycerine

<?php
header('Location: http://www.example.com/');
?>

Make sure there is nothingabove the line outputted, otherwise it will fail.

确保输出的行上方没有任何内容,否则会失败。

回答by Mystical

You could use ob_startalong with the headerfunctionin order to prevent redirection errors like so:

您可以将ob_start标头函数一起使用,以防止重定向错误,如下所示:

<?php
   ob_start(); // Starts Output Buffering 
   header(/*Your redirection's location*/); // Redirects user to given location.
?>