title: How to safely get full URL of parent directory of current PHP page
body: <p>I'm using:</p>
  
  <pre><code>$domain = $_SERVER['HTTP_HOST'];
  $path = $_SERVER['SCRIPT_NAME'];
  $themeurl = $domain . $path;
  </code></pre>
  
  <p>But this of course gives the full URL.
  Instead I <strong>need the full URL minus the current file and up one directory and minus the trailing slash</strong>.</p>
  
  <p>so no matter what the browser URL domain is eg localhost, https://, http://, etc that the full real (bypassing any mod rewrites) URL path of the parent directory is given without a trailing slash. </p>
  
  <p>How is this done?
  Safely so no XSS as I guess (from reading) using anything but 'SCRIPT_NAME' has such risk.. not sure though ofc.. just been reading a ton trying to figure this out.</p>
  
  <p>examples:
  if given:</p>
  
  <pre><code>https://stackoverflow.com/questions/somequestions/index.php
  </code></pre>
  
  <p>need:</p>
  
  <pre><code>https://stackoverflow.com/questions
  </code></pre>
  
  <p>without the trailing slash.</p>
  
  <p>and should also work for say:</p>
  
  <pre><code>http://localhost/GetSimple/admin/load.php
  </code></pre>
  
  <p>to get</p>
  
  <pre><code>http://localhost/GetSimple
  </code></pre>
  
  <p>which is what I'm trying to do.</p>
  
  <p>Thank you.</p>
  
  <p><hr><hr>
  <strong>Edit:</strong>
  Here's the working solution I used:</p>
  
  <pre><code>$url  = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
  $url .= $_SERVER['SERVER_NAME'];
  $url .= htmlspecialchars($_SERVER['REQUEST_URI']);
  $themeurl = dirname(dirname($url)) . "/theme";
  </code></pre>
  
  <p>it works perfectly.</p>