Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

I am back with a simple question (or related question).

The question is simple however I have not received an answer yet. I have asked many people with different experience in PHP. But the response I get is: "I don't have any idea. I've never thought about that." Using Google I have not been able to find any article on this. I hope that I will get a satisfying answer here.

So the question is:

What is the difference between $_SERVER['DOCUMENT_ROOT'] and $_SERVER['HTTP_HOST'] ?

Are there any advantages of one over the other?

Where should we use HTTP_HOST & where to use DOCUMENT_ROOT?

like image 635
Aakash Sahai Avatar asked Aug 29 '11 12:08

Aakash Sahai


People also ask

What is $_ server DOCUMENT_ROOT?

$_SERVER['DOCUMENT_ROOT'] returns. The document root directory under which the current script is executing, as defined in the server's configuration file.

What is Http_host?

The HTTP_HOST is obtained from the HTTP request header and this is what the client actually used as "target host" of the request. The SERVER_NAME is defined in server config.


4 Answers

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.

like image 131
2 revs Avatar answered Oct 31 '22 00:10

2 revs


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?

like image 20
nothrow Avatar answered Oct 31 '22 00:10

nothrow


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.

like image 38
J0HN Avatar answered Oct 30 '22 22:10

J0HN


<?php include($_SERVER['DOCUMENT_ROOT'] . '/include/abc.php') ?>

should be used for including the files in another file.

header('Location: '. $_SERVER['HTTP_HOST'] . '/abc.php')

should be used for hyperlinking

like image 31
Adi Avatar answered Oct 31 '22 00:10

Adi