if i have a menu such as
<a href="index.php">home</a>
<a href="aboutus.php">about us</a>
<a href="contact.php">contact us</a>
inside a file called menu.php
and this menu.php file gets include into pages all across the application. but what if i create a new folder and call it portfolio and in this folder there is a file named example1.php and the menu.php gets included in this file. the home,about us, and contact us links wont work anymore since their pointing to the root. so i created this function
function getRoot() {
$self = $_SERVER['PHP_SELF'];
$protocol = $_SERVER['https'] == 'on' ? 'https:/' : 'http:/';
$docroot_before = explode("/", $self);
array_pop($docroot_before);
$docroot_after = implode("/", $docroot_before);
$host = $_SERVER['HTTP_HOST'];
return $protocol.$host.$docroot_after."/";
}
and the menu now looks like this
<a href="<?=getRoot();?>index.php">home</a>
<a href="<?=getRoot();?>aboutus.php">about us</a>
<a href="<?=getRoot();?>contact.php">contact us</a>
the link should come out as http://localhost/domain/aboutus.php (for example)
but it comes out as http://localhost:/domain/portfolio/aboutus.php (and this is wrong).
what is the best way to always get the correct doc root no matter where the menu.php gets include.?