title: Path of current PHP script relative to document root
body: <p><strong>TL;DR</strong>: What alternatives to the last code sample on this page are there when trying to use relative links on pages included with PHP's <code>include</code> command?</p>
  
  <hr>
  
  <p>I'm trying to <code>include()</code> a file that links to a <code>.css</code> document, the problem is that I need to include this file from multiple (different) directories. My directory structure looks somewhat like this:</p>
  
  <pre><code>www
  ├── (getpath.php)
  │
  ├── css
  │   └── style.css
  ├── php
  │   └── header.php
  └── content
      ├── index.php
      └── posts
          └── index.php
  </code></pre>
  
  <p>(I'm including <code>header.php</code> in both <code>index.php</code> files. <code>header.php</code> is the file that refers to <code>style.css</code> (which the browser needs to be able to find). Example: When using <code>../css/style.css</code>, the CSS will show up in my <code>content/index.php</code> but not in my <code>content/posts/index.php</code>, because the browser is looking for <code>content/css/index.php</code>.)</p>
  
  <p>The website should also work when placed in a <strong>subdirectory</strong> of the document root, not in the document root itself. For this reason, I can't just use <code>/css/style.css</code> (relative to the server root) to link to my CSS stylesheet.</p>
  
  <p><em>I think</em> (please correct me if I'm wrong!) that the easiest way to tell the browser where to find the CSS document is probably to specify the path <strong>relative to the document root</strong>. As I expect my website to be in a <strong>subdirectory</strong> of the document root (and want to avoid using a <code>config</code> file to store the subdirectory in), I need to find out the relative path of the subdirectory the website is in.
  My idea was to place a file called <code>getpath.php</code> in my website's root directory (not the document root). In that file, I wrote the following:</p>
  
  <pre><code>&lt;?php
    define('PROJECT_SUBDIRECTORY', $_SERVER['PHP_SELF']);
  ?&gt;
  </code></pre>
  
  <p>However, I noticed that this gives me the relative directory of the PHP file that the user requested in the browser (e.g. <code>content/index.php</code>) instead of the included file (<code>getpath.php</code>). I've also read that <code>PHP_SELF</code> is based on <code>REQUEST_URI</code>, which wouldn't be secure. I've found this solution <a href="https://stackoverflow.com/a/2370883/1717025">here</a>:</p>
  
  <pre><code>define('PROJECT_DIR', preg_replace('/^' . preg_quote($_SERVER['DOCUMENT_ROOT'], '/') . '/', '', __DIR__));
  </code></pre>
  
  <p>It works perfectly fine, but I'm wondering if there's a "better" way to do this.
  I didn't manage to find a variable for that — is the solution above really the only way to do that?</p>
  
  <p><strong>Should I use the previous code example or is there a better way?</strong><br>
  Should I change my directory structure? How do other people solve this problem?</p>
  
  <hr>
  
  <p><strong>Edit</strong>: I have multiple (completely independent) websites on my webserver, they're all in subdirectories of the document root (like <code>/documentroot/project1/</code>, <code>/documentroot/project2/</code>, ...)<br>
  I'm trying to get the websites to work in subdirectories. I could obviously send CSS to the browser via <code>/css/style.css</code>, but that doesn't work if the website is in a subdirectory. I also want to avoid using a config file to specify the path of my project relative to the document root.</p>
  
  <hr>
  
  <p><strong>Another edit</strong>: The problem is that every php file in my project <code>/documentroot/project1</code> includes a navigation bar by using PHP's <code>include</code> function. The navigation bar is saved as <code>/documentroot/project1/php/header.php</code> and the css for the navbar as <code>/documentroot/project1/css/style.css</code>. When using relative links (to tell the browser where <code>style.css</code> is), the browser still thinks the path is relative to <code>index.php</code> rather than <code>header.php</code>, so it searches for the CSS in the same directory as <code>index.php</code>.<br>
  Problem: There are multiple index.php files in different directories, and I don't want to copy &amp; paste my CSS folder in every single directory that contains a <code>index.php</code> file.</p>