I have a header.php file which contains a .css file link.
When I \"include\" header.php into another php file in different fo
I can see one reason for wanting to have dynamic and relative path generation for href links, and that is if you run your project on multiple domains or sites that have different paths. (For example, your project is available on http://myproject.example.org/ and also on http://example.org/myprojecttest/). If this is not the case, I would suggest directly specifying your css includes relative to the root folder:
If this does apply to you, try this:
In every top-level document that requires header.php, add a $ROOT variable that indicates the top-level document's location compared to the root. e.g.:
$ROOT = './';
or
$ROOT = '../';
or
$ROOT = '../../';
Now, in your header.php file, you can use:
This allows you to make a header.php file that will work for any page at any relative path.
Full Example
Included File (/path/header.php)
[...]
File 1 (/path/index.php):
File 1 (/path/admin/index.php):
File 3 (/path/admin/test/magic.php):