Does $_SERVER[“SERVER_NAME”] and $_SERVER[“DOCUMENT_ROOT”] always aim to the same directory?

送分小仙女□ 提交于 2019-12-11 22:09:35

问题


Do $_SERVER["SERVER_NAME"]; and $_SERVER["DOCUMENT_ROOT"]; always resolve to the same directory? Is there a scenario where they wouldn't?

EDIT:

To clarify, I KNOW these are not the same thing- I'm trying to establish only whether SERVER_NAME will always map to the DOCUMENT_ROOT. i.e., if by following each of them, one will always arrive at same physical location on the servers file system.


回答1:


  • SERVER_NAME is internet vieable address - stackoverflow.com
  • DOCUMENT_ROOT is absolute URL to current directory - in server /data/stackoverflow/ ...

but yes, it DOES aim to the same directory (everytime)




回答2:


SERVER_NAME is not a path it should be the actual domain name. You may want to include REQUEST_URI which would give you the absolute path from the perspective of the webserver (assuming you're just using files. REQUEST_URI gives you the path as entered by the user. The webserver or PHP itself can deliver content that isn't even in a file)

For that it really depends on what's in $some_relative_path

DOCUMENT_ROOT is the absolute server path to where your web sites files actually live on the server. DOCUMENT_ROOT may be useful for including other PHP files but it should never be exposed to the connecting client as that is a security risk.

They should never be the same in any scenario, unless for some reason someone setup / on the server as the document root. That would likely be a very bad idea because then even system files would be exposed to the web.




回答3:


SERVER_NAME is the name of the server where the script is been executed. It also could be the virtual host name.

DOCUMENT_ROOT is the path to the script you are executing. If you are running some kind of url rewriting, it could be the same value as SERVER_NAME



来源:https://stackoverflow.com/questions/7449367/does-serverserver-name-and-serverdocument-root-always-aim-to-the-sa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!