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.

Leave a Comment