whats a good way of always having the correct doc root string?
<p>if i have a menu such as</p>

<pre><code>&lt;a href="index.php"&gt;home&lt;/a&gt;
&lt;a href="aboutus.php"&gt;about us&lt;/a&gt;
&lt;a href="contact.php"&gt;contact us&lt;/a&gt;
</code></pre>

<p>inside a file called menu.php</p>

<p>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</p>

<pre><code>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."/";
}
</code></pre>

<p>and the menu now looks like this</p>

<pre><code>&lt;a href="&lt;?=getRoot();?&gt;index.php"&gt;home&lt;/a&gt;
&lt;a href="&lt;?=getRoot();?&gt;aboutus.php"&gt;about us&lt;/a&gt;
&lt;a href="&lt;?=getRoot();?&gt;contact.php"&gt;contact us&lt;/a&gt;

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).
</code></pre>

<p>what is the best way to always get the correct doc root no matter where the menu.php gets include.?</p>
