Q:

Nested `include()` directives - How to include a file (which includes another) from more than one directories?

I have these three files to work with:
document_root/include/config.php
document_root/include/database.php
document_root/index.php
And the relevant part of the file contents are like below:
config.php
// ...
$MyVar = 100;
// ...

database.php
// ...
require('config.php');
// Aiming to use the definitions in "config.php" here
// ...

index.php
// ...
require('include/database.php');
// Using the code in "database.php" here
// ...

The problem is that, config.php is somehow not included, and yet no error message is given (in E_ALL mode). I can't access to the definitions in the config.php file from database.php file while running the code in index.php.
In my PHP.ini file include_path is set to C:\...\document_root\include directory.
An PHP version is 5.3.0.
I have observed that, if I change the require() directive in database.php as
require('include/config.php');
the code runs without any failure and everything is just fine. But this solution is not possible in practice, because I'm planing to include the config.php file from multiple locations.
What is the cause of this problem?
How can I fix it?
Any help will be appreciated.

A:

The cause of this problem is, that include resolves relative filenames based on the working directory, not based on the directory where the file is located that does the include.
The working directory is the directory of the PHP file that is launched by the webserver (in your case index.php I guess). If that file includes some other file, the working directory is not changed. It can be changed manually using chdir, but you should not do that solely for the sake of an include. 
One possible solution is to use
include dirname(__FILE__) . DIRECTORY_SEPARATOR . '[RELATIVE_PATH]';

where [RELATIVE_PATH] is the relative path from the file that does the include to the file that is included.
In PHP 5.3, __DIR__ can be used instead of dirname(__FILE__).

