[英]PHP header() only refreshing on relative file path
I ran into a curious situation where my page would not refresh properly upon submitting a form.我遇到了一个奇怪的情况,即我的页面在提交表单时无法正确刷新。
Form ( page.php
):表单( page.php
):
<form action="funcs/foo.php" method="post">
<!-- various inputs -->
<input type="submit" value="Post it">
</form>
And the function ( foo.php
):和函数( foo.php
):
// statements updating database, changing page layout...
header("Location:$_SERVER[DOCUMENT_ROOT]/page.php");
By changing the header to the following, page.php is refreshed.通过将标题更改为以下内容,page.php 被刷新。
header("Location:../page.php");
My question is why?我的问题是为什么? What's the difference between specifying the full path and the relative path?指定完整路径和相对路径有什么区别?
header("Location:$_SERVER[DOCUMENT_ROOT]/page.php"); header("位置:$_SERVER[DOCUMENT_ROOT]/page.php");
First, you reference array index wrong (it should be quoted: $_SERVER['DOCUMENT_ROOT']
) but aside from this you use it wrong as Location
never operates on paths in context of your local filesystem as you incorrectly assume.首先,您引用了错误的数组索引(应该引用: $_SERVER['DOCUMENT_ROOT']
),但除此之外,您使用它是错误的,因为Location
永远不会像您错误地假设的那样对本地文件系统上下文中的路径进行操作。 So if you want to use relative, ie /foo
it does NOT mean this is foo
folder in root of your filesystem, but it is foo
relative to document root
.因此,如果您想使用相对,即/foo
它并不意味着这是文件系统根目录中的foo
文件夹,而是foo
相对于document root
。 So passing DOCUMENT_ROOT
gives you wrong path, unreachable for the browser.所以传递DOCUMENT_ROOT
会给你错误的路径,浏览器无法访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.