title: Dynamically add the same CSS file from different pages at different directory depths
body: <p>Okay I've been trying to figure this out for a while now.
  I have a website I'm working on and here is basically my structure:</p>
  
  <pre><code>index.php
  page-x/
      index.php
      page-x-y/
          index.php
  include/
      functions.php
  css/
      main.css
  </code></pre>
  
  <p>Except add on more sub-folders and index pages.
  Basically pages at three different levels.</p>
  
  <p>They all need to have css/main.css, but the path from any one index page is going to be different.</p>
  
  <p>Right now I use functions.php to add the CSS, and already have to manually enter the relative path from an index page to functions.php (ie <code>require_once("../include/functions.php");</code> or <code>../../include</code> etc</p>
  
  <p>How can I make it so that functions.php can figure out the relative path from any index page it is included on, to css/main.css?</p>
  
  <p>I.E. how would I define <code>$relDir</code> for the following code in functions.php:</p>
  
  <pre><code>echo '&lt;link rel="stylesheet" type="text/css" href="' . $relDir . 'css/main.css" /&gt;';
  </code></pre>
  
  <p>I could manually pass <code>""</code> or <code>"../"</code> or <code>"../../"</code> every-time to the function, but can I avoid that?</p>
  
  <hr>
  
  <p><strong>EDIT 3</strong>
  If I define <code>$BASE = dirname(__DIR__);</code> in functions.php, I can find the root of the site and go from there to get the absolute path to the CSS.</p>
  
  <p>However the css doesn't seem to link properly, on my local machine the resulting link looks like: (I can manipulate the slashes to be consistent but it doesn't help)</p>
  
  <pre><code>"C:\Documents and Settings\Lucas\site/css/main.css"
  </code></pre>
  
  <p>(Note: I am using an <strong>alias</strong> on easyPHP so that I can connect to this address from <code>http://127.0.0.1:8888/site/</code> but<code>__DIR__</code> finds the ACTUAL directory of the file.</p>
  
  <p>And on the (My universities) server I am testing on comes back with</p>
  
  <pre><code>"/Volumes/Web/Students/MYNAME/Sites/MYSITE/css/main.css"
  </code></pre>
  
  <p>The ACTUAL URL of the website looks like:</p>
  
  <pre><code>http://www.SCHOOL.com/MYNAME/MYSITE/
  </code></pre>
  
  <p>The CSS wont link in either situation</p>
  
  <hr>
  
  <p><strong>EDIT 2</strong> I can define <code>$relDir</code> as:</p>
  
  <pre><code> str_repeat('../', substr_count($_SERVER['REQUEST_URI'], '/');
  </code></pre>
  
  <p>returns one of the following depending on how deep the index is:</p>
  
  <pre><code>""
  "../"
  "../../"
  </code></pre>
  
  <p>This a lot of processing apparently though. It sounds like the best thing to do would be to look into proper testing environments, and use an absolute path. I'll look into that. I'm still a bit of a newb with some of that stuff.</p>
  
  <hr>
  
  <p><strong>EDIT 1:</strong> Okay most people are suggesting I stick with just doing it manually, originally had a function call something like this:</p>
  
  <pre><code>&lt;?php require_once("../include/functions.php");
  buildHead("Music", "../"); //($title, $relDir) ?&gt;
  </code></pre>
  
  <p>I just have to change the <code>../</code> to whatever in both places for every page.</p>