It is possible to means by PHP to obtain the hostname and document root in the server? Let me do that without storing them as variables inside your php code. I'm unwilling to use $_SERVER because I've heard it's not reliable and susceptible to attack. How is this done on the virtual host? Does a trusted and safe method exist?

You could attempt

$docroot = getenv("DOCUMENT_ROOT");

[cde] allows you access atmosphere variables. You can observe all variables that are offered by printing [cde]. Maybe [cde] likewise helps.

getenv is reliable but phpinfo isn’t (see Chris Shiflett’s SERVER_NAME Versus HTTP_HOST). Only when Apache’s [cde] is enabled the canonical title is proven.

As if you pointed out, you can get : http://php.net/manual/en/reserved.variables.php There is a breakdown here of utilizing realpath() and $_SERVER to obtain the doc root: http://www.helicron.net/php/

Also, you can professional('hostname') or something like that similar to obtain the hostname.