title: PHP how to get the base domain/url?
body: <pre><code>function url(){
      if(isset($_SERVER['HTTPS'])){
          $protocol = ($_SERVER['HTTPS'] &amp;&amp; $_SERVER['HTTPS'] != "off") ? "https" : "http";
      }
      else{
          $protocol = 'http';
      }
      return $protocol . "://" . $_SERVER['HTTP_HOST'];
  }
  </code></pre>
  
  <p>For example with the function above, it works fine if I work with the same directory, but if I make a sub directory, and work in it, it will give me the location of the sub directory also for example. I just want <code>example.com</code> but it gives me <code>example.com/sub</code> if I'm working in the folder <code>sub</code>. If I'm using the main directory,the function works fine. Is there an alternative to <code>$_SERVER['HTTP_HOST']</code>?</p>
  
  <p>Or how could I fix my function/code to get the main url only? Thanks.</p>