开发者

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

开发者 https://www.devze.com 2023-04-05 16:00 出处:网络
Do $_SERVER[\"SERVER_NAME\"]; and $_SERVER[\"DOCUMENT_ROOT\"]; always resolve to the same directory?Is there a scenario where they wouldn\'t?开发者_运维百科

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.


  • 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)


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号