PHP Include Path
A common way for PHP coders to organise their include files is to put them in a separate folder – called ‘includes’, for example.
Suppose that you have a file called connection.php that contains your database connectivity details (server IP, username and password, and the path to it is something like this:
http://yourdomain.com/includes/connection.php
How do you refer to the path in your code? Well, you could use the explicit path given above, but that’s no good if you want your code to run on another domain. Think about the WordPress platform, for example. It needs to run on many different domains, so an explicit path to the includes folder just wouldn’t work. The path to the includes folder would need to be different for each domain WordPress was installed on.
Enter the magic of $_SERVER['DOCUMENT_ROOT'].
$_SERVER is an array that contains server related information, and $_SERVER['DOCUMENT_ROOT'] holds the (you guessed it) document root of your web server. You can then specify the path to a specific includes file from that location.
To refer to the connection.php file mentioned above, you would use
include $_SERVER['DOCUMENT_ROOT'].'/includes/connection.php';