PHP标头位置:../location与$ _SERVER [DOCUMENT_ROOT]/location [英] PHP Header Location: ../location vs $_SERVER[DOCUMENT_ROOT]/location

查看:80
本文介绍了PHP标头位置:../location与$ _SERVER [DOCUMENT_ROOT]/location的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我进行了简单的PHP登录,但是当我尝试像这样重定向时:

So, I made a simple PHP login, but when I tried to redirect like this:

$path = $_SERVER["DOCUMENT_ROOT"];  
header("Location: $path/admin/index.php");

它似乎什么也没做,但是刷新页面后我已经登录.
在将代码更改为此之后:

it seemed like it did nothing, but after I refreshed the page I was logged in.
After I changed my code to this:

header("Location: ../admin/index.php");

有效.

有人可以向我解释一下吗?

Could someone please explain this to me?

Ps.对不起,我的英语不好

Ps. sorry for my bad english

推荐答案

标头发送到浏览器,因此它不是内部服务器操作.由于它不是内部重定向,因此您无需处理内部路径.使用DOCUMENT_ROOT时,您将获得文件所在目录的内部服务器路径.

The header is sent to the browser, so it is not an internal server maneuver. And with it not being an internal redirect, you don't deal with internal paths. When you use DOCUMENT_ROOT you will get the internal server path to the directory where your files are located.

如果要引用站点的根目录作为URL,只需使用/.

If you want to reference the root of the site as a URL, just use /.

header("Location: /admin/index.php");
header("Location: /"); # go to homepage, for example

您的..之所以起作用,是因为您可能位于子目录中,并且..已转换为admin所在的父目录.

Your .. worked because you probably were on a subdirectory, and .. was translated to the parent directory which is where admin is.

这篇关于PHP标头位置:../location与$ _SERVER [DOCUMENT_ROOT]/location的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆