php $_SERVER['DOCUMENT_ROOT'] 和 $_SERVER['HTTP_HOST'] 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7229783/
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
Difference between $_SERVER['DOCUMENT_ROOT'] and $_SERVER['HTTP_HOST']
提问by Aakash Sahai
I am back with a simple question (or related question).
我回来了一个简单的问题(或相关问题)。
The question is simple however I have not received an answer yet. I have asked many people with different experience in PHP. But the response I get is: "I don't have any idea. I've never thought about that." Using Google I have not been able to find any article on this. I hope that I will get a satisfying answer here.
问题很简单,但我还没有收到答案。我问过很多在 PHP 方面有不同经验的人。但我得到的回应是:“我不知道。我从来没有想过这个。” 使用谷歌我无法找到任何关于此的文章。希望在这里能得到满意的答复。
So the question is:
所以问题是:
What is the difference between $_SERVER['DOCUMENT_ROOT']
and $_SERVER['HTTP_HOST']
?
$_SERVER['DOCUMENT_ROOT']
和 和有$_SERVER['HTTP_HOST']
什么区别?
Are there any advantages of one over the other?
一个比另一个有什么优势吗?
Where should we use HTTP_HOST & where to use DOCUMENT_ROOT?
我们应该在哪里使用 HTTP_HOST 以及在哪里使用 DOCUMENT_ROOT?
回答by John Ledbetter
DOCUMENT_ROOT
The root directory of this site defined by the 'DocumentRoot' directive in the General Section or a section e.g.
DOCUMENT_ROOT
该站点的根目录由“通用部分”中的“DocumentRoot”指令定义或某个部分,例如
DOCUMENT_ROOT=/var/www/example
HTTP_HOST
The base URL of the host e.g.
HTTP_HOST
主机的基本 URL,例如
HTTP_HOST=www.example.com
The document root is the local path to your website, on your server; The http host is the hostname of the server. They are rather different; perhaps you can clarify your question?
文档根目录是您网站的本地路径,位于您的服务器上;http 主机是服务器的主机名。它们相当不同;也许你可以澄清你的问题?
Edit: You said:
编辑:你说:
Case 1 : header('Location: '. $_SERVER['DOCUMENT_ROOT'] . '/abc.php')
Case 2: header('Location: '. $_SERVER['HTTP_HOST'] . '/abc.php')
案例 1 : header('Location: '.$_SERVER['DOCUMENT_ROOT'] .'/abc.php')
案例2:header('位置:'。$_SERVER['HTTP_HOST'] .'/abc.php')
I suspect the first is only going to work if you run your browser on the same machine that's serving the pages.
我怀疑只有当您在提供页面服务的同一台机器上运行浏览器时,第一个才会起作用。
Imagine if someone else visits your website, using their Windows machine. And your webserver tells them in the HTTP headers, "hey, actually, redirect this location: /var/www/example/abc.php." What do you expect the user's machine to do?
想象一下,如果其他人使用他们的 Windows 机器访问您的网站。你的网络服务器在 HTTP 标头中告诉他们,“嘿,实际上,重定向这个位置:/var/www/example/abc.php。” 你希望用户的机器做什么?
Now, if you're talking about something like
现在,如果你在谈论类似的事情
<?php include($_SERVER['DOCUMENT_ROOT'] . '/include/abc.php') ?>
vs
对比
<?php include($_SERVER['HTTP_HOST'] . '/include/abc.php') ?>
That might make sense. I suspect in this case the former is probably preferred, although I am not a PHP Guru.
这可能是有道理的。我怀疑在这种情况下,前者可能更受欢迎,尽管我不是 PHP 大师。
回答by nothrow
Eh, what's the question? DOCUMENT_ROOT
contains the path to current web, in my case /home/www
. HTTP_HOST
contains testing.local
, as it runs on local domain. The difference is obvious, isn't it?
呃,有什么问题吗?DOCUMENT_ROOT
包含当前网络的路径,在我的例子中/home/www
。HTTP_HOST
contains testing.local
,因为它在本地域上运行。区别很明显,不是吗?
I cannot figure out where you could interchange those two, so why should you consider advantages?
我不知道你在哪里可以互换这两个,所以你为什么要考虑优势?
回答by J0HN
HTTP_HOST
will give you URL of the host, e.g. domain.com
HTTP_HOST
会给你主机的 URL,例如 domain.com
DOCUMENT_ROOT
will give you absolute path to document root of the website in server's file system, e.g. /var/www/domain/
DOCUMENT_ROOT
将为您提供服务器文件系统中网站文档根目录的绝对路径,例如 /var/www/domain/
Btw, have you tried looking at PHP's manual, specifically $_SERVER
? Everything is explanied there.
顺便说一句,你有没有试过查看 PHP 的手册,特别是$_SERVER
?一切都在那里解释。
回答by Adi
<?php include($_SERVER['DOCUMENT_ROOT'] . '/include/abc.php') ?>
should be used for including the files in another file.
应该用于将文件包含在另一个文件中。
header('Location: '. $_SERVER['HTTP_HOST'] . '/abc.php')
should be used for hyperlinking
应该用于超链接
回答by Suhasini
if you want domain path like 'example.com', you can use "HTTP_HOST"if you want folder '/public_html/foldername/'path you can use "DOCUMENT_ROOT"
如果你想要像'example.com'这样的域路径,你可以使用“HTTP_HOST”如果你想要文件夹'/public_html/foldername/'路径你可以使用 “DOCUMENT_ROOT”
回答by Tahridabbas
$_SERVER ['HTTP_HOST']
returns the domain url
a.g. www.example.com
While $_SERVER['DOCUMENT_ROOT']
returns the roof of current web..
Such as
$_SERVER ['HTTP_HOST']
返回域 url ag www.example.com 而$_SERVER['DOCUMENT_ROOT']
返回当前网络的屋顶.. 如
回答by John
$_SERVER ['HTTP_HOST']
is defined by the clientand may not even be set! You can repeat a request and withhold the header for local testing in developer tools such as for Waterfox/Firefox. You mustdetermine if this header is set and if the host being requested exists (one of the very first things you do, even before starting to send any of your headers) otherwise the appropriate action is to kill the entire process and respond with an HTTP 400 Bad Request
. This goes for allserver-side programming languages.
$_SERVER ['HTTP_HOST']
由客户端定义,甚至可能没有设置!您可以重复请求并保留用于在开发工具(例如 Waterfox/Firefox)中进行本地测试的标头。您必须确定是否设置了此标头以及所请求的主机是否存在(您要做的第一件事之一,甚至在开始发送任何标头之前),否则适当的操作是终止整个进程并以HTTP 400 Bad Request
. 这适用于所有服务器端编程语言。
$_SERVER['DOCUMENT_ROOT']
is defined by the server as the directory which the executing script is located. Examples:
$_SERVER['DOCUMENT_ROOT']
由服务器定义为执行脚本所在的目录。例子:
public_html/example.php
=public_html/
public_html/test1/example.php
=public_html/test1/
public_html/example.php
=public_html/
public_html/test1/example.php
=public_html/test1/
Keep in mind that if you're using Apache rewrites that there is a difference between the $_SERVER['REQUEST_URI']
(the URL requested) and $_SERVER['PHP_SELF']
(the file handling the request).
请记住,如果您使用 Apache 重写,则$_SERVER['REQUEST_URI']
(请求的 URL)和$_SERVER['PHP_SELF']
(处理请求的文件)之间存在差异。
回答by JesusTCS
The Title question is perfectly awnsered by John Ledbetter.
约翰·莱德贝特 (John Ledbetter) 完美地解决了标题问题。
This awnser is intended to expand and offer additional information about what seems to be the original poster inner concerns:
此 awnser 旨在扩展并提供有关似乎是原始海报内部关注点的其他信息:
- Where would make sense to use the URLbased location: $_SERVER['HTTP_HOST']?
- Where would make sense to use the localbased location: $_SERVER['DOCUMENT_ROOT']?
- Where both can be used, what are the Advantages and Disadvantages of each one. ?
- 在哪里使用基于URL的位置有意义:$_SERVER['HTTP_HOST']?
- 在哪里使用基于本地的位置有意义:$_SERVER['DOCUMENT_ROOT']?
- 两者都可以使用的地方,每一种的优点和缺点是什么。?
Following my awnsers:
跟随我的 awnsers:
- By usign the HTTP_HOSTyou can abstract yourself from the machine Folder System which means in cases where portability is a concern and you are expected to install the Application on multiple servers potentially with diferent OS this approach could be easier to maintain.
- You can also take advantage of HTTP_HOSTif your server is going to become unavailible and you want a diferent one from the cluster to handle the request.
- By Using the DOCUMENT_ROOTyou can access the whole filesystem (depends on the permissions you give to php) it makes sense if you want to access a program which you dont want to be accesible from the web or when the Folder System is relevant to your Application.
You can also take advantage of DOCUMENT_ROOTto get the subsite root instead of the Host.
$_SERVER['HTTP_HOST'] = "www.example.com"; $_SERVER['DOCUMENT_ROOT'] = "var/www/domain/subsite1" // equivalent to www.example.com/subsite1
- 通过使用HTTP_HOST,您可以将自己从机器文件夹系统中抽象出来,这意味着在考虑可移植性的情况下,您需要将应用程序安装在可能具有不同操作系统的多台服务器上,这种方法可能更容易维护。
- 如果您的服务器将变得不可用,并且您想要一个与集群不同的服务器来处理请求,您也可以利用HTTP_HOST。
- 通过使用DOCUMENT_ROOT,您可以访问整个文件系统(取决于您授予 php 的权限),如果您想访问不想从网络访问的程序或文件夹系统与您的应用程序相关时,这是有意义的.
您还可以利用DOCUMENT_ROOT来获取子站点根而不是主机。
$_SERVER['HTTP_HOST'] = "www.example.com"; $_SERVER['DOCUMENT_ROOT'] = "var/www/domain/subsite1" // equivalent to www.example.com/subsite1