Difference between $_SERVER['DOCUMENT_ROOT'] and $_SERVER['HTTP_HOST']

DOCUMENT_ROOT

The root directory of this site defined by the 'DocumentRoot' directive in the General Section or a section e.g.

DOCUMENT_ROOT=/var/www/example 

HTTP_HOST

The base URL of the host e.g.

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?

Edit: You said:

Case 1 : header('Location: '. $_SERVER['DOCUMENT_ROOT'] . '/abc.php')

Case 2: header('Location: '. $_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?

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.


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?

I cannot figure out where you could interchange those two, so why should you consider advantages?


HTTP_HOST will give you URL of the host, e.g. 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/

Btw, have you tried looking at PHP's manual, specifically $_SERVER? Everything is explanied there.

Tags:

Php