Does $_SERVER[“SERVER_NAME”] and $_SERVER[“DOCUME

2019-09-06 11: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