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:
I suspect the first is only going to work if you run your browser on the same machine that's serving the pages.Case 1 : header('Location: '. $_SERVER['DOCUMENT_ROOT'] . '/abc.php')
Case 2: header('Location: '. $_SERVER['HTTP_HOST'] . '/abc.php')
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. DOCUMENT_ROOT or SERVER_NAME for showing CSS in subfolders
HTML is not aware of the PHP script which generated it.
The best way to solve it is to set virtual server ( /etc/hosts
on Linux, %windir%/system32/drivers/hosts
on Windows, may need restart)
127.0.0.1 localhost
127.0.0.1 vm1.localhost
and then in httpd.conf<VirtualHost *:80>
ServerName localhost
DocumentRoot "/htcdocs"
</VirtualHost>
<VirtualHost *:80>
ServerName vm1.localhost
DocumentRoot "/htcdocs/yourfolder"
</VirtualHost>
After this modification (and restart of your server you can access your pages by http://vm1.localhost/
, the DOCUMENT_ROOT is set to yourfolder
Note: after setting the DOCUMENT_ROOT, I recommend to use
href="/bootstrap/css/bootstrap.min.css"
instead ofhref="<?php echo $_SERVER['DOCUMENT_ROOT'] ?>/bootstrap/css/bootstrap.min.css"
creating HTML paths using PHP system variables may force you to rewrite the code when you move to hosting with different settings, whereas pure HTML remains independent. What's the difference between $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME']?
Difference
http://sandbox.phpcode.eu/g/3e38d.php/test
Script name is absolute path to file. PHP_SELF
is script you're currently in (along with "path" after .php
)
It's like $_SERVER['SERVER_NAME']
and $_SERVER['HTTP_HOST']
http://sandbox.phpcode.eu./g/f5093.php
http://sandbox.phpcode.eu/g/f5093.php
spot one difference Is safe to access to $_SERVER['DOCUMENT_ROOT'] directly?
filter_input()
?
There is no input in $_SERVER['DOCUMENT_ROOT']
, that is a server controlled property and can not be manipulated ordinarily by user input so no need to filter it using that method.
Unless you think your server is compromised that is :) in which case it will be useless to filter for anything at all.
How to use definitions (define variable) in include
"Yes it's working on localhost, but why it's not working the $_SERVER['DOCUMENT_ROOT'] on live website? It returns me only "/" not something like "student.sps-prosek.cz/....";, is it better to use $path = $_SERVER['HTTP_HOST''] ?"
I understand your problem, the problem is when you deploy to a server the url will change.
try this code
<?php
define('PROJECT_BASE_FOLDER' , substr($_SERVER['REQUEST_URI'], 0, 8) == '/sample/' ? '/sample/' : '');
//sample is the name of the folder. the 0 to 8 is how many char your project name has including the '/'
define('PROJECT_FOLDER' , substr($_SERVER['REQUEST_URI'], 0, 8) == '/sample/' ? '' : '');
define('HTTP' , 'http://');
define('BASE_PATH' , $_SERVER['DOCUMENT_ROOT'] . PROJECT_BASE_FOLDER . PROJECT_FOLDER);
define('SITE_URL' , HTTP . $_SERVER['SERVER_NAME'] . str_replace($_SERVER['DOCUMENT_ROOT'] . '/', '', BASE_PATH));
// or ':8080/' is the port you are using in your local machine
define('SITE_URL' , HTTP . $_SERVER['SERVER_NAME'] . ':8080/' . str_replace($_SERVER['DOCUMENT_ROOT'] . '/', '', BASE_PATH));
therefore in your code you will use <?php
require_once "config.php";
include_once BASE_PATH ."/index.php";
?>
This should work as I am using this logic so that I will not have to change the directory every time I need to deploy to a server. PHP $_SERVER['DOCUMENT_ROOT']
$_SERVER['DOCUMENT_ROOT'] returns
You could useThe document root directory under which the current script is executing, as defined in the server's configuration file.
$_SERVER['HTTP_HOST']
or absolute paths like <a href="/nmc/Admin/LoginPage.php">Login</a>
Related Topics
How to Select a MySQL Database to Use with Pdo in PHP
Save Accents in MySQL Database
What Are Available Solutions of a Browser/Mobile Phone Detection
How to Use a PHP Class from Another File
Get Folders with PHP Glob - Unlimited Levels Deep
PHP Gd: How to Get Imagedata as Binary String
Selenium2 Firefox: Use the Default Profile
How to Run a Whois Lookup with PHP or Python
PHP - Get Array Value with a Numeric Index
File_Get_Contents('Php://Input') Always Returns an Empty String
How Would You Transform a Pre-Existing Web App into a Multilingual One
Laravel 4 - Including a "Partial" View Within a View (Without Using Blade Template)
Imagemagick Brew Installation with PHP Module in MAC Os X
How to Run PHP File Using Cron Jobs
Calling a Stored Procedure from Codeigniter's Active Record Class