如何在 PHP 中重定向到同一页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8130990/
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
How to redirect to the same page in PHP
提问by user710502
How can I redirect to the same page using PHP?
如何使用 PHP 重定向到同一页面?
For example, locally my web address is:
例如,本地我的网址是:
http://localhost/myweb/index.php
How can I redirect within my website to another page, say:
如何在我的网站内重定向到另一个页面,例如:
header("Location: clients.php");
I know this might be wrong, but do I really need to put the whole thing? What if later it is not http://localhost/
?
我知道这可能是错误的,但我真的需要把整个事情都放在一起吗?如果以后不是http://localhost/
呢?
Is there a way to do something like this? Also, I have a lot of code and then at the end after it is done processing some code... I am attempting to redirect using that. Is that OK?
有没有办法做这样的事情?另外,我有很多代码,然后在处理完一些代码后最后......我正在尝试使用它进行重定向。这可以吗?
采纳答案by Nicole
There are a number of different $_SERVER
(docs) properties that return information about the current page, but my preferred method is to use $_SERVER['HTTP_HOST']
:
有许多不同的$_SERVER
( docs) 属性可以返回有关当前页面的信息,但我的首选方法是使用$_SERVER['HTTP_HOST']
:
header("Location: " . "http://" . $_SERVER['HTTP_HOST'] . $location);
where $location
is the path after the domain, starting with /
.
$location
域之后的路径在哪里,以/
.
回答by Syntax Error
My preferred method for reloading the same page is $_SERVER['PHP_SELF']
我首选的重新加载同一页面的方法是 $_SERVER['PHP_SELF']
header('Location: '.$_SERVER['PHP_SELF']);
die;
Don't forget to die or exit after your header();
不要忘记在你的 header(); 之后死亡或退出;
Edit: (Thanks @RafaelBarros )
编辑:(感谢@RafaelBarros)
If the query string is also necessary, use
如果还需要查询字符串,请使用
header('Location:'.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
die;
Edit: (thanks @HugoDelsing)
编辑:(感谢@HugoDelsing)
When htaccess url manipulation is in play the value of $_SERVER['PHP_SELF'] may take you to the wrong place. In that case the correct url data will be in $_SERVER['REQUEST_URI']
for your redirect, which can look like Nabil's answer below:
当 htaccess url 操作起作用时, $_SERVER['PHP_SELF'] 的值可能会把你带到错误的地方。在这种情况下,正确的 url 数据将$_SERVER['REQUEST_URI']
用于您的重定向,这可能类似于下面 Nabil 的回答:
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
exit;
You can also use $_SERVER[REQUEST_URI] to assign the correct value to $_SERVER['PHP_SELF']
if desired. This can help if you use a redirect function heavily and you don't want to change it. Just set the correct vale in your request handler like this:
如果需要,您还可以使用 $_SERVER[REQUEST_URI] 分配正确的值$_SERVER['PHP_SELF']
。如果您大量使用重定向功能并且不想更改它,这会有所帮助。只需在您的请求处理程序中设置正确的值,如下所示:
$_SERVER['PHP_SELF'] = 'https://sample.com/controller/etc';
回答by Nabil Kadimi
Another elegant one is
另一个优雅的是
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
exit;
回答by squarecandy
To really be universal, I'm using this:
为了真正通用,我正在使用这个:
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off'
|| $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://';
header('Location: '.$protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
exit;
I like $_SERVER['REQUEST_URI']
because it respects mod_rewrite and/or any GET variables.
我喜欢,$_SERVER['REQUEST_URI']
因为它尊重 mod_rewrite 和/或任何 GET 变量。
https detection from https://stackoverflow.com/a/2886224/947370
回答by Lead Developer
header('Location: '.$_SERVER['PHP_SELF']);
will also work
也会工作
回答by digout
A quick easy approach if you are not concerned about query params:
如果您不关心查询参数,一个快速简单的方法:
header("location: ./");
回答by user10141671
I just tried using header("Location: ");
(without any value) and it redirected to the current page.
我只是尝试使用header("Location: ");
(没有任何值)并将其重定向到当前页面。
回答by Sergey
Simple line below works just fine:
下面的简单行工作得很好:
header("Location: ?");
回答by Raimundo
I use correctly in localhost:
我在本地主机中正确使用:
header('0');