Calling $_SERVER['SCRIPT_NAME'] from files deeper in the hierarchy
<p>(Preliminary note: There are seemingly many variants of this question already here, but they all seem to focus on the <strong>filesystem</strong> location of the physical PHP files themselves, not the <strong>web</strong> URL location of things, which is what I'm after.)</p>

<hr>

<p><strong>INTRO 1:</strong></p>

<p>I have a small "website" with the following physical <strong>filesystem</strong> structure:</p>

<pre><code>variable.php
folder
  test.php
</code></pre>

<p>(In other words, <code>variable.php</code> in the top-level, and <code>test.php</code> in the folder <code>folder</code>.)</p>

<p>Here are the file contents:</p>

<pre><code>&lt;?php
//variable.php
$basePath = dirname($_SERVER['SCRIPT_NAME']);
?&gt;

&lt;?php
//test.php
include("../variables.php");
echo $basePath;
?&gt;
</code></pre>

<hr>

<p><strong>INTRO 2:</strong></p>

<p>When developing this "website" locally, it is on this address:</p>

<pre><code>http://localhost/~noob/test-website
</code></pre>

<hr>

<p><strong>QUESTION:</strong></p>

<p>The problem is that if I put <code>http://localhost/~noob/test-website/folder/test.php</code> into my browser, it prints out <code>/~noob/test-website/folder/</code>.</p>

<p>I want it to print out <code>/~noob/test-website</code> (the "web location" of variable.php). In other words, I want to have global access from all files, <strong>even ones deeper in the filesystem hierarchy</strong>, to <code>/~noob/test-website</code>. How do I accomplish this?</p>

<p>I obviously do not want to hardcode this path in. The reason is that when I upload it onto a production server, the location changes to something more sane like <code>http://example.com/</code> (and I don't want to have to modify this hardcoded path after uploading to the server, hence this question of course).</p>
